ReferenceError: bot is not defined - discord

This is something with the const or var. I think that it won't work unless I recode it. Here is my problem:
bot.on('ready', () => {
^
ReferenceError: bot is not defined
at C:\Users\Dylan\Desktop\discord bot\app.js:7:1
at ContextifyScript.Script.runInThisContext (vm.js:50:33)
at Object.runInThisContext (vm.js:139:38)
at Module._compile (module.js:607:28)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Function.Module.runMain (module.js:684:10)
at startup (bootstrap_node.js:187:16)
Here is my code:
const Discord = require('discord');
const bot = new Discord.Bot();
const fs = require('fs')
const commandsList = fs.readFileSync('Storage/commands.txt','utf8');
});
bot.on('ready', () => {
// This event will run if the bot starts, and logs in, successfully.
console.log(`Bot has started, with ${client.users.size} users, in ${client.channels.size} channels.`);
});
bot.on('message', message => {
if (message.content === '!ping') {
message.channel.send('pong');
}
});
bot.on('message', message => {
if (message.content === '!apply') {
message.channel.send('GOGLE STUFF');
}
});
bot.on('message', message => {
if (message.content === '!server') {
message.channel.send('LA');
}
});
bot.on('message', message => {
if (message.content === '!do you know dae wae') {
message.channel.send('Yes I know dae wae brother');
}
});
bot.on('message', message => {
if (message.content === 'do you have a bot?') {
message.channel.send('no');
}
});
bot.on('message', message => {
if (message.content === 'who is the owner') {
message.channel.send('FantasmicNerd, duh');
}
});
bot.on('message', message => {
if (message.content === 'can i be staff' || message.content === 'can I be staff?' || message.content === 'can i be staff?') {
message.channel.send('Application - lalalalalala');
}
});
bot.on('message', message => {
if (message.content === 'somebody touch my spaghet') {
message.channel.send('SOMEBODY TOUCH YOUR SPAGHET!');
}
});
bot.on('message', message => {
if (message.content === 'so how about that airline food') {
message.channel.send('HAAHAHAHHAHAHAHHAHAHAHAHAHHAHAHHAHAHAHAHHAHAHAAHAHAHHAHAHAHHAHAHAHAHAHHAHAHHAHAHAHAHHAHA');
}
});
bot.on('ready',() => {
console.log('Bot Launched...')
bot.user.setStatus('Online')
bot.user.setActivity('on The Magical')
});
bot.on('message', message => {
if (message.content === '?help' || message.content === '?Help') {
message.channel.send('I have messaged you the commands and prefix.');
}
});
bot.on('message', message => {
if (message.content === '?help' || message.content === '?Help') {
message.author.sendMessage(commandsList);
}
});
bot.on('ready', function() {
bot.user.setUsername("The Magical");
// THIS MUST BE THIS WAY
bot.login('NDA0NjYzNzIwNDQzMzc5NzEy.DUapFw.zjDvPkG4QxZJ1rdxDYaPZEaVpiM');
I have looked all over for answers but nothing seems to work. I end up with this in cmd prompt when I try to run it

Where you're requiring the library you need to change it to discord.js to get access to the library.
Also the library does not expose a class called "Bot" so you need change that to "Client".
I'm guessing the )}; on line 5 is supposed to be closing the ready event on line 88.
But I suggest you just stick with one ready event, so put the status and activity methods in the first ready event and remove the other ones. Also, if you've set your bot username to what you want I would remove that method, or put a check to see if the username is already set to what you want, and if it is then don't change it.
Same with all the message events as the ready ones, stick to one message event and just chain all the if statements in that one. Example:
bot.on('message', message => {
if (message.content === '!ping') {
message.channel.send('pong');
}
else if (message.content === '!apply') {
message.channel.send('GOGLE STUFF');
}
...
});
Also you need to remember what you define your variables as. In the first ready event you use client.users instead of what you have defined, which is bot.

According to discord.js API document, there is no Bot class.
Maybe what you need is
const client = new Discord.Client();
I suggest you check the document and example for your application.

Related

Message correction privilege error discord bot

When the code works, the message 'Cannot edit a message authorized by another user' appears. How can I edit the message that the bot wrote?
client.on('messageCreate', async (message) => { // When a message is created
if (message.author.bot) return;
if(message.content === '!ping') { // If the message content is "!ping"
let timecc = 10*60
await message.reply("⏳"+parseInt((timecc / 60)/60)+"h"+parseInt((timecc / 60)%60)+"m"+(timecc % 60)+"start")
var timer = setInterval(() => {
timecc -= 1
message.edit({ content: "⏳"+parseInt((timecc / 60)/60)+"h"+parseInt((timecc / 60)%60)+"m"+(timecc % 60)+"s" })
.catch(console.error);
if (timecc === 0) {
clearInterval(timer);
message.edit({ content: "⌛"+"time out" })
.catch(console.error);
}
}, 1000)
message is the message that emitted the listener, the user's message.
To reference the bot's response you must assign the promise.
const botMsg = await message.reply(...);
botMsg.edit(...)

I'm trying to make my bot send messages to a specific channel by using channel id

This is the code i used:
bot.on("message", function (message) {
if (message.content == '.shift') {
const channel01 = bot.channels.cache.find(channel => channel.id === "ChannelIDhere")
channel01.send('Text');
}
});
However it doesn't work for me, Most likely it is Outdated, after all the reference i found is a year ago, or i simply made a mistake.
Edit:
client.on('msg', msg => {
if (msg.content === '.go') {
const channel01 = client.channels.cache.get("872654795364765756");
channel01.send('Text');
}
});

Message on Reaction discord.js

I been trying to make my bot reply to users when they react to the bots emoji but no luck I hope someone can help me here, I'm just a beginner with discord.js.
here's my code I just need help with the part where the bot sends a message back once the user reacts to it.
bot.on('message', message => {
if(message.author.bot)
{
if(message.embeds)
{
const embedMsg = message.embeds.find(msg => msg.title === 'Next step');
if(embedMsg)
{
message.react('708923041928839169')
.then(reaction => reaction.message.react('708923028846805073'))
.catch(err => console.error);
}
}
return;
}
if(message.content.toLowerCase() === 'done'){
const embed = new Discord.MessageEmbed();
embed.setTitle('Next step')
embed.setColor(colors.blue);
embed.setDescription('Please react to Agree or Disagree');
message.channel.send(embed)
}
});
bot.login(token);
You can simply use a ReactionCollector to do this. The documentation for this can be found easily on the discord.js docs, and I would recommend you look there when unsure of how to do something before asking a question on StackOverflow.
Here's an example using your code:
bot.on('message', message => {
if(message.author.bot) return;
if(message.content.toLowerCase() === 'done'){
const embed = new Discord.MessageEmbed();
embed.setTitle('Next step')
embed.setColor(colors.blue);
embed.setDescription('Please react to Agree or Disagree');
message.channel.send(embed).then(embedMsg => {
embedMsg.react('708923041928839169')
.then(reaction => embedMsg.react('708923028846805073'))
.catch(err => console.error);
const filter = (r, u) => r.emoji.id == '708923041928839169' || r.emoji.id == "708923028846805073";
const collector = message.createReactionCollector(filter, {time: 60000});
collector.on('collect', (r, u) => {
//Put your response to reactions here
message.channel.send("Reply with something to " + u.tag + " because they reacted with " + r.emoji.name);
});
})
}
});
bot.login(token);
I also moved your code in which you add the reactions to the Embed, because the way you were doing it was an unnecessary extra step.
Relevant resources:
https://discord.js.org/#/docs/main/stable/class/Message?scrollTo=createReactionCollector

Deleting a channel using reactions discord.js

I have a little question.
I'm currently in the progress of trying out creating a tickets bot, and had the idea of having so when a user types !close, it would present him with an embed, asking him if he really does want to close it using reactions (:wastebasket: for Yes, :x: for No).
If the user reacts for Yes, the channel will close. As for No, the embed message will be deleted.
I will be glad to help.
You can use something a little like this:
const prevMsg = message
message.channel.send(embed).then(message => {
const questionMessage = message;
questionMessage.react('🗑️')
.then(() => questionMessage.react('❌'));
const filter = (reaction, user) => {
return ['🗑️', '❌'].includes(reaction.emoji.name) && user.id === prevMsg.author.id;
};
questionMessage.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '🗑️') {
// closing function here
} else {
questionMessage.delete();
}
})
})
If it didn't work, please let me know!

This spams dms how do I make it stop after one message Discord.js

const Discord = require('discord.js');
const client = new Discord.Client();
client.once('ready', () => {
console.log('AC0');
client.user.setActivity('Tornaments', { type: 'WATCHING'}).catch(console.error);
});
const tornament = new Discord.MessageEmbed()
.setColor('#000ff')
.setTitle('AC0 Tornaments')
.setDescription(`1. TORNAMENT1
2. TORNAMENT2
3. TORNAMENT3
4. TORNAMENT4`)
client.on('message', message => {
if (message.author.id === '694644198531661844') {
if (message.content === '>endac0') {
process.exit();
}}
if (message.content === '>tornaments') {
message.channel.send(tornament);
}
if (message.content.startsWith('>tornamentsadd')){
client.users.fetch('694644198531661844').then(user => {
user.send(`${message.content.toString()}`)
});}
});
client.login('')
How would I make it not spam me I don't think there is a loop or anything in it also I don't know what else to add so yeah I will just keep typing
You just have to add a return function at the end.
if (message.content.startsWith('>tornamentsadd')){
client.users.fetch('694644198531661844').then(user => {
return user.send(`${message.content.toString()}`)

Resources