I am writing my first discord bot with help from a tutorial. I am stuck because my bot hasn't been able to respond to commands and I've checked the tutorials and my code many times over. Is there something I'm doing wrong?
const discord = require('discord.js');
const client = new discord.Client();
const prefix = '!';
client.once('ready' , () => {
console.log('Zach Is Bad is online');
});
client.on('message', message =>{
if (!message.content.startswith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'ping'){
message.channel.send('pong!');
}
});
client.login('redacted')
The only issue with your code is message.content.startswith(prefix).
It is startsWith, it's case sensitive.
if (!message.content.startsWith(prefix) || message.author.bot) return;
There is a typo at client.on('ready', () => ...), but this shouldn't be the cause of the error.
You wrote if (!message.content.startswith(prefix) || message.author.bot) return; but I'm pretty sure startwith must be startWith (caps are importants). Try modifying this, then restart your bot.
If it doesn't work put console.log("test OK") right before the ping command, restart and send !ping. If there is "test OK" in your bot logs, then the problem come from the ping command. If you doesn't see this log, try moving the console.log line before the if statement. This is a simple yet effective method to know where does a problem come from.
Related
i'm new to programming and started making a discord bot by watching a few tutorials. I want the bot to DM the discord embed to the user who types "-buy" in a text channel. When running the code, the bot comes online and runs the "your bot name is online!" but no DM is sent. I would greatly appreciate any help. Thanks
const Discord = require('discord.js');
const client = new Discord.Client();
const prefix = '-';
client.once('ready', () => {
console.log('your bot name is online!');
});
client.on('message', message =>{
if(message.author.client) return;
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLocaleLowerCase();
if(command === 'buy'){
const testEmbed = new Discord.MessageEmbed()
.setColor(0x7f03fc)
.setTitle('test embeddy')
.setDescription('test description woo')
.setFooter('this is the footer')
try {
message.author.send(testEmbed);
} catch {
message.reply('Sorry I cannot message you! Check if your DMs are public!')
}
}
});
client.login('');
the token isn't the problem, i deleted it so i could upload here
The message.author.client returns the bot client, and it doesn't return a boolean. So your bot is blocked from there. Try removing that code and write message.author.bot that returns a boolean if the message author is a bot user. It will work.
I'm trying to make a command for my Discord bot which the bot replies regardless if the user says some other text inside the command triggering message, like an 8ball command. Is this possible? If you don't know what I mean, here's an example
if ("message.content === "*8ball");
message.channel.send(variablename)
User: *8ball am i cool
Bot: 🎱 | yes
I'm guessing I have to change message.content to something different?
client.on("message", (message) => {
const prefix = "-";
const arguments = message.content.slice(prefix.length).trim().split(/ +/g);
const commandName = arguments.shift().toLowerCase();
if (message.content.startsWith(prefix) && commandName == "8ball") {
message.channel.send("reply here");
};
});
So it's my first ever time coding and i'm creating a discord bot. It's all been going fine until I try to run the bot.js file on commmand line (using "node bot.js")
But It just comes up with a bunch of errors.
My Code:
const Discord = require('discord.js');
const client = new Discord.Client();
const auth = require('./auth.json');
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`)
});
client.login(auth.token);
client.on('message', msg => {
if (msg.content === 'ping') {
msg.reply('pong');
}
});
Question: Will you re-post you error picture? When you click on it it says the page doesn't exist.
PLEASE READ ALL BEFORE MAKING CHANGES!
First (Bad) Guess: But without the picture, I would guess (and this is not a good guess) that it's because "client.login(auth.token)" isn't at the bottom. Another guess is that ".content ===" does nothing. You should try and remove ".content" to see if it then works.
Here is your code with just that change:
const Discord = require('discord.js');
const client = new Discord.Client();
const auth = require('./auth.json');
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}`)
});
client.on('message', msg => {
if (msg.content === 'ping') {
msg.reply('pong')
}
});
client.login(auth.token);
Logging bot is ready: The is also some other things I think you should change, this changing "client.user.tag" to "client.user.username" to instead show the bot's username. Another thing is "msg.content" I'm pretty sure this does nothing, and should be changed to just "===", there are some other ones, but that's my favorite one because it's the least amount of characters and easiest to type.
Here is your code with all of these changes:
const Discord = require('discord.js');
const client = new Discord.Client();
const auth = require('./auth.json');
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}`)
});
client.on('message', msg => {
if (msg === "ping") {
msg.reply('pong')
}
});
client.login(auth.token);
Token (& patrik's answer): (No hate to patrik) What patrik says that putting your token in the actual script will help (It won't, and makes it easier to hack), now while I do this, I really don't care if my bot gets hacked, it's in one server. He/She also says that the token error means that discord.js can't get the token, this is a node.js error, not a discord.js error. You probably messed up on writing a piece of code, that is likely in "auth.json". You should probably re-run through your files before doing any of these changes.
A auth/config/token (token file) .json file should look like this:
{
"token":"TOKEN-HERE"
}
And then it should be used by doing
const auth|config|token = require(./auth|config|token.json);
client.login(auth.token);
I hope this helps with coding your bot!
It's because the node.js version is not updated to be compatible with the new discord.js version
First, install old version of discord.js in console type
npm i discord.js.old#11.6.4
In your script change
this:
const Discord = require('discord.js')
To:
const Discord = require('discord.js.old')
Glad to see you are into making bots too!
I would firstly suggest replacing all those "client" words with "bot"
The unexpected token might be because of that above mentioned thing or that your token is not just simply there.
Remove line:
client.login(auth.token);
and replace it with:
bot.login('YOUR-TOKEN-HERE');
You can check what if your token at the Discord Developer page
The entire command works fine, when executed it shows what expected, however it can be executed in any channel when it should only be executable within a specific channel.
Ive tried basically everything that popped into my mind
const Discord = require('discord.js');
const guilds = require('../data/guilds.json');
module.exports.run = async (bot, message, args) => {
if (!message.channel.id === guilds[message.guild.id].botChannelID)
return;
var img
if (args[0] == 'aea') {
img = bot.utils.randomSelection([
I'm really stupid and probably made a dumb mistake so if anyone could please help me with this that would be great
Try to replace:
if (!message.channel.id === guilds[message.guild.id].botChannelID)
To:
if (message.channel.id !== guilds[message.guild.id].botChannelID)
You used a „!“ in front of the message.channel.id although you should use it in your „===“.
const Discord = require("discord.js");
const prefix = ("d");
const client = new Discord.Client ();
client.on("ready", async () => {
console.log(`${client.user.username} is now working online!`);
});
client.on("message", message => {
if(message.author.bot) return;
if(message.channel.type === "dm") return;
let messageArray = message.content.split(" ");
let command = messageArray[0];
let args = messageArray.slice(1);
if(!command.startsWith(prefix)) return;
});
client.login("MY TOKEN");
When I try doing the command: "dhelp" that I've made and coded for example, it doesn't work. Why is this? People have said that I need a command handler and I think it's done but don't why it is still not working, why is this? Please help me, I really need help right now.
Well, to start off, it doesn't actually look like your code does anything regardless of what the value of command is. Perhaps using if/else statements would be a good place to start.
if (command === "help") {
/* some code here */
}
where /* some code here */ is the code for your bot.