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
Related
Hi this is my code for my discord bot, I try to run it and it comes up without an error, even the "im alive" log comes up in console, but nothing else, thought it was my token but the bot logs in,
the only thing I can think of is the client.on but even in doc's I cant see the diffrence.
Im new to js so anybody's help is appreciated
const Discord = require("discord.js");
const client = new Discord.Client({intents: [Discord.Intents.FLAGS.GUILDS]});
const prefix = '!';
client.once('ready', () => {
console.log("I'm alive!");
});
client.on('message', message => {
console.log('asd');
if(message.content = '${prefix}hi'){
console.log('command detected');
message.channel.send('Hi ${message.author}');
}
});
client.login('my-token');
For getting the message data, you need to enable the GUILD_MESSAGES intent in your client as well as you need to go to your Discord Developer page and then in your application, go to the Bot option and the check the Message Content Intent
I was trying to write in an embed in my discord music bot command to tell the author of the command when and what the bot was playing.
Now, I've tried passing in different arguments (such as messageembed itself) and this isn't working.
I've only been coding for maybe 6 months now, so I'm not the best
here is my code (the bit that I'm having trouble with at least)
const song_queue = queue.get(guild.id);
if (!song) {
song_queue.voice_channel.leave();
queue.delete(guild.id);
return;
}
const stream = ytdl(song.url, {filter: 'audioonly'});
song_queue.connection.play(stream, {seek: 0, volume: 0.5})
.on('finish', () => {
song_queue.songs.shift();
video_player(guild, song_queue.songs[0]);
});
const replyEmbed = new Discord.MessageEmbed()
.setColor('#FF2D00')
.setTitle('🎶 Now Playing 🎶')
.setDescription(`${song.title} [${message.author}]`)
await song_queue.text_channel.send(replyEmbed)
}
It looks like the code that you have is just a snippet taken from your file, but from the error I can assume that Discord has not been defined.
Please make sure to include this at the top of your file:
const Discord = require("discord.js");
It seems you may have forgotten to require discord.js. Do this like so:
const Discord = require('discord.js');
If you have got something that looks like this at the top of the file, but you have a different constant name, i.e. not Discord, replace Discord in Discord.MessageEmbed() with whatever your constant is called.
I am trying to make a bot that would add a role that is in the server when a person types !join choiceOutOfVariousRoles. I am currently using discord version 12. My error message is:
fn = fn.bind(thisArg);
Although trying various techniques I could not get the code to work.
const Discord = require('discord.js');
const client= new Discord.Client();
const token = process.env.DISCORD_BOT_SECRET
client.on('ready', () => {
console.log("I'm in");
console.log(client.user.username);
});
client.on('message', msg => {
if (msg.content.toLowerCase().startsWith("!join"))
{
var args = msg.content.toLowerCase().split(" ")
console.log(args)
if (args[1] === 'sullen')
{
msg.channel.send('You have successfully joined Sullen!')
const sullenRole = msg.guild.roles.cache.find('name','Sullen')
msg.member.addRole(role.id)
}
}
});
client.login(token)
**EDIT: Fixed what everyone was saying and all I need to do now Is update the permissions, (my friend has to do that because its not my bot) and I should be all good. Thanks everyone! :D
discord.js introduces breaking changes very frequently, and v12 is no exception. You need to make sure you find up-to-date code otherwise it won't work. GuildMember#addRole was moved to GuildMemberRoleManager#add, which means you must use msg.member.roles.add(sullenRole).
I have tried many different ways of formatting the code, however, whenever I add code so that I must provide a reasoning to ban someone, I am always given an Internal Server Error. Here is my code.
module.exports.run = async (client, message, args) => {
const member = message.mentions.members.first();
const reason = args.slice(1).join(" ")
if (!message.member.hasPermission("BAN_MEMBERS")) {
return message.reply("you lack sufficiant permissions to execute this command.");
} else if (member.hasPermission("ADMINISTRATOR")) {
message.reply("you cannot ban this member.")
}
member.ban(reason).then((member) => {
message.channel.send(`${member} has been banned.`);
});
I use a command handler, and all my other commands work fine.
first step: Define the user
let user = message.mentions.members.first() || message.guild.members.cache.get(args.join(' '));
Second step: Create embed message or normal message
const userbanned = new Discord.MessageEmbed()
.setColor('#FF0000')
.setAuthor('User Banned')
.setDescription(`**${user.user.username}#${user.user.discriminator}** is now banned from this server`)
.setFooter(`bot_name`);
Third step: Send message
user.send(`You were banned from **${message.guild.name}** by ${message.author.username}#${message.author.discriminator}`)
return user
.ban()
.then(() => message.channel.send(userbanned))
.catch(error => message.reply("ERROR"));
Try changing
member.ban().then((member) =>//
to
member.ban({reason : args.slice(1).join(' ')}).then((member) =>//
Here is my code
import Discord from 'discord.js'
const bot = new Discord.Client()
const channelID = 'the channel's id'
bot.login('my token')
bot.on('ready', async () => {
const channel = (await bot.channels.fetch(channelID)) as Discord.TextChannel //im using typescript
console.log(channel.messages.cache.array())
})
For some reason the array is empty, even though there are tons of messages in the channel. Any reason this might be? I know that it is getting the right channel, as I've tested that and it shows the right server and name. Thanks.
Use channel.messages.fetch().array() instead. Your array is most likely empty because the messages are not cached, optionally you can also cache the messages by doing channel.messages.fetch({}, true).array()