i tried to add a command handler, but command files are showing incorrect values, for example the bot's ping is showing up as NaN and uptime/users/servers are all showing as zeros. the command was working fine until i moved it into it's own command file. i'm not sure what the problem is as no errors occur in the console. please help, the code to my index.js and one command file is below
my best guess as to where the issue is:
module.exports = {
name: bot,
description: displays bot information.,
execute(message, args) {
//code
}
https://pastebin.com/hJ4nessW
It looks like your bot.js file has some lines without using Discord.js's Embed class...
At the first part where's
const Discord = require(`discord.js`);
const bot = new Discord.Client(); // <- here
isn't necessary because it's defined in index.js, so should be use from index.js
switch (args[0]) {
case `ping`:
bot.commands.get(`ping`).execute(message, args, bot /* use it */ );
break;
case `bot`:
bot.commands.get(`bot`).execute(message, args, bot /* use it */ );
break;
}
and bot.js
execute(message, args, bot) {
// You can use bot variable
// ...
I recommend to avoid spaghetti code, because it looks worse and spends a lot of time... I made the shorten, less lines and working code version -> https://pastebin.com/sRNacWYt
Related
To avoid having to change every emoji from every file whenever I switch an emoji, I decided to place an emojis.json and call the emoji from there.
emojis.json
{
"loading": "<a:loading:847653387343626301>",
"pressf": "<:pressf:862166705911758909>"
}
Exampleping.js
const emoji = require('../emojis.json')
module.exports = {
name: 'ping',
execute(message, args) {
message.channel.send(`${emoji.loading}Pong.`)
}
}
Is this the right way? I'm open to new/better ideas.
Btw it errors: code: 'MODULE_NOT_FOUND',
Well, your approach is correct to a certain extent. Only issue is that you have imported a json file instead of js file and hence it throws an error.
Correct way of achieving this, is having a emojis.js file with your json object exported using module.exports
// emojis.js
module.exports = {
loading: "<a:loading:847653387343626301>",
pressf: "<:pressf:862166705911758909>"
};
// Exampleping.js
const emojis = require('../../emojis.js')
module.exports = {
name: 'ping',
execute(message, args) {
message.channel.send(`${emoji.loading}Pong.`)
}
}
Your code is clean except your import statement is not referring to the emojis.json file. To make it more clear, the script failed to locate the file, which means emojis.json is not located inside the same directory as exampleping.js (require('./emojis.js')).
Based on my experience with Discord bot development, I believe you placed emojis.json in your root directory while exampleping.js is placed inside a directory commonly named "commands". With that being said, all you need is to exist the command directory by adding another . to require().
const emojis = require('../emojis.js');
//instead of ./emojis.json
See HTML File Path
Is it possible to use the client/bot constant across different files?
My index.js file has gotten pretty clogged up, so I tried splitting different functions into different files.
I already tried exporting the bot constant from index.js:
// in index.js
module.exports = {
Gbot: bot
}
// in different file
const index = require('../index.js')
const bot = index.Gbot
bot.on('message', message => {
message.channel.send("test")
})
The second file does not do anything, it does not respond with "test"
There is no errors either
Is this not possible or am I doing something wrong?
This is possible, but why do you want to do that? If you are using a Command Handler you define client or bot to use it everywhere. And if not you are running everything in index.js, the file where you defined client or bot.
Edit:
//index.js
module.exports.Gbot = bot;
//other file
const index = require("../index.js");
const bot = index.Gbot;
So I am working on my bot, and for my ;say command I have the following code. The issue is, that when a user does something like ";say hi" it repeats as ;say on one line, goes down a line, and on that line it says hi. Before I added in a command handler, the command worked fine, however only now am I running into issues.
The code (including vars + command handler portion) -
const Prefix = require('./../../config.json');
module.exports = {
name: "say",
description: "Says the user input",
execute (Client, message, Args) {
let Say = message.content.substring(({Prefix} + 'say').Args).split(" ");
let Output = Say.splice(1);
message.channel.send(Output);
message.delete({ timeout: 1 });
},
};
in case it comes of necessity for some reason, my config.json file (might be an issue with the prefix) is in the "container" directory in the below file location -
container/commands/fun/say.js
At first, why don't you use message.content.substring(`${Prefix}say`); instead of these two lines
let Say = message.content.substring(({Prefix} + 'say').Args).split(" ");
let Output = Say.splice(1);
that would be the same, no?
I also think if you have a problem like this it's probably because there's something missing in the event handling part of your code.
Also you need to select the prefix from your config.json file, by adding .prefix or the name of your variable in your .json file after the require('./../../config.json'); not to get the entire object.
I'm working on a discord bot for a D&D game. I want the bot to be able to recognize when someone has recently used a command. This is what I have so far;
const barid = require("./tavernarray.json")
var tavernarray = barid.guests;
message.channel.send('Tavern Keeper Emotes and replies;')
if (message.member.roles.cache.has('770678762944725003')){
if (tavernarray.includes(message.author.id)){
message.channel.send("Weren't you just here?");
} else;
tavernarray.push(message.author.id);
message.channel.send(` Welcome to the Tavern Guild Master ${message.author}`);
setInterval (() => {
tavernarray.pop(message.author.id)
}, 30000)
} else {
message.channel.send("Error no role");
}
From what I can tell the code works in that on the first command, we get the welcome message expected and the user ID is added to the array. On the second command though, there's a short delay and then we get both messages. Should I be using setTimeout instead of setInterval? Or is it an issue with using a .json array? I tried keeping the array in the program, but it just kept resetting every time I ran it.
Yes, you should be using setTimeout(). The reason you might have an issue is because the code is trying to remove the variable from the JSON array every 30 seconds which will cause two issues: higher memory usage and potential errors. The difference between setInterval() and setTimeout() is timeout executes the function once, whilst the other loops continuously until it is told to break. Along with this, the way you're using else is the issue. When you use else; (Take into note the semi colon), you're telling the code that if the ID isn't there, it shouldn't execute any code, as a semi colon indicates the end of a line of code. A snippet is shown below
if (tavernarray.includes(message.author.id)){
message.channel.send("Weren't you just here?");
} else { // Instead of ; we'll use {}
tavernarray.push(message.author.id);
message.channel.send(` Welcome to the Tavern Guild Master ${message.author}`);
setInterval (() => {
tavernarray.pop(message.author.id)
}, 30000)
}
I have recently rewrote my bot using switch case
But now it doesnt respond to the commands
Instead, it just sits there and does nothing
Here is the code's link: https://sourceb.in/ff321fd803
the problem is here
const args = msg.content.slice('2').trim().split(' ');
const command = args.shift().toLowerCase();
Slice would be better have as argument a number and not a string because it could cause an error if it's not possible to convert it into a number
You cannot access "command" with the args variable since you removed the command from it.
Args isn't well defined.
Use this :
const args = msg.content.split(' ').slice(1)
const command = msg.content.split(' ').shift().toLowerCase()