this.on("messageReactionAdd", async (reaction, user) => {
if(reaction.emoji.name === "✔️") {
console.log("User reacted")
}
})
This is my code and at the moment it will not even run the event
this is defined as client.
My tips: Try if the ready or the message event works. If it doesn't, try removing the if statement in your code. If it still doesn't work try using the starter code from the discord.js guide which is something like this:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('messageReactionAdd', (reaction, user) => {
console.log('event works!');
});
client.login('your-token-here');
If it still doesn't work your bot is most likely not logging in to discord. In this case I'd suggest you to recheck if your bot's token is the right one.
Related
I'm trying to code a Discord bot that would read embed messages sent by another bot and ping a specific role if the embed contains '(FREE)' in the title, as displayed here:
https://i.stack.imgur.com/kPsR1.png
Unfortunately, my code produces nothing. I don't run into any error, the bot simply doesn't post a message when conditions are matched to do so — yet it is online and has the permissions to post messages in the channel.
Any help would be really appreciated. I went through all the questions related to this topic on SO, but I couldn't find a working solution to my issue. I'd like to mention that I'm a beginner in JS.
Thank you so much.
const Discord = require('discord.js');
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES", "GUILD_WEBHOOKS"] })
client.on('ready', () => {
console.log(`Logged in...`);
});
client.on('messageCreate', (message) => {
if (message.embeds) {
const embed = message.embeds[0]
if (embed.title === '(FREE)') {
return message.channel.send('#Free mint')
}
}
})
Edit: I believe my question is different from the one suggested in the comment as I'm looking for specific content in the embed.
I managed to make the bot work with the following code, thanks #Gavin for suggesting it:
const Discord = require('discord.js');
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES", "GUILD_WEBHOOKS"] })
client.on('ready', () => {
console.log(`Logged in...`);
});
client.on('messageCreate', message => {
for (let embed of message.embeds) {
if (embed.title.includes('FREE')) {
return message.channel.send("<#&" + roleId + ">")
}
}
});
I am trying to make my bot send a message to a channel in all servers that it is in when updated. My idea is that I will trigger this manually whenever I update my bot. For example, I would like to have it send a message such as "Updated bot to version 2.03. Update log: (changes made in the update)". This is the code I currently have:
const Discord = require('discord.js');
const client = new Discord.Client();
var Long = require("long");
const getChannel = (guild) => {
// get "original" default channel
if(guild.channels.cache.has(guild.id))
return guild.channels.cache.get(guild.id)
const jimmyChannel = guild.channels.cache.find(channel => channel.name === "jimmybot");
if (jimmyChannel)
return jimmyChannel;
return guild.channels.cache
.filter(c => c.type === "text" &&
c.permissionsFor(guild.client.user).has("SEND_MESSAGES"))
.sort((a, b) => a.position - b.position ||
Long.fromString(a.id).sub(Long.fromString(b.id)).toNumber())
.first();
}
// I found this as an example but I'm not sure if it will work
client.on("guildMemberAdd", member => {
const channel = getChannel(member.guild);
channel.send(`text`);
});
client.login('token');
My question is how do I call this manually instead of it sending the message whenever a user joins the server?
If I'm understanding this properly...
Client is actually just a variation of a Node.js EventEmitter, as stated here.
This means that you can trigger the events of your own accord, and so here you could do your own 'custom' EventEmitter and trigger it of your own accord.
Ex code paired with message event emitter. I know that it's generally frowned upon to put an event emitter inside of another, but it was the only solution I could find.
const Discord = require('discord.js');
const client = new Discord.Client();
token = 'XXX'
client.once('ready', () => {
console.log('Fully functional and initialized!');
});
// Attach a listener function
client.on('test', console.log);
client.on('message', msg => {
if (msg.author.bot) return;
if (msg.content === 'emit event') {
// Emit the event
client.emit('test', 'This will be printed on the console');
}
})
client.login(token);
The ways I've tried.
Loop through tokens, this is fine but not much room to customise what bot does what. Example having each bot type in a specific channel, with this I've realised the bots type in the same location right after each other.
const auth = require('./tokens.json')
const Discord = require('discord.js')
for (const token of auth.Tokens) {
const client = new Discord.Client()
client.on('ready', () => {
console.log('I am ready !')
console.log(client.user.id)
})
client.login(token)
}
Also by creating multiple instances of a discord.client
const Discord = require('discord.js');
const client1 = new Discord.Client();
const client2 = new Discord.Client();
ect...
client1.once('ready',() => {
})
client1.on('message', async(message) => {
})
client2.once('ready',() => {
})
client2.on('message', async(message) => {
})
client1.login(CONFIG.Token1);
client2.login(CONFIG.Token2);
I am just wondering if there is other ways of doing this, lets say I have 5-6 bots and I do the 2nd method the code will get quite long depending on what i want to add into it.
I did think about adding a loop something like this.
for(var i = 0; i < token.length; i++)
And having a channel id linked to a specific number as the i++ is increasing it. So each bot would get its own number and channel id, but I'm not sure if that's even a thing that would work or if it would be good enough to use.
Any suggestions would be greatly appreciated and thank you for reading.
I think your second method:
creating multiple instances of a discord.client
Would be the best way to do it.
The easiest way to give the bots specific channels is to change their discord permissions and physically assigning them the channels.
Another way could be something like:
client.channelID = <ID of channel you want the bot in>
client2.channelID = <ID of channel you want the bot in>
// ... code
client.on('message', message => {
if (message channel.id !== client.channelID) return;
// ...
I was able to run multiple bots from the same script using this code:
const auth = ['TOKEN1', 'TOKEN2']
const Discord = require('discord.js')
for (const token of auth) {
const client = new Discord.Client()
client.on('ready', () => {
console.log('I am ready !')
console.log(client.user.id)
});
client.on('message', (msg) => {
if (msg.content === '!ping') {msg.reply('pong!')}
});
client.login(token);
}
I want to send dm to anyone who has the role of "xxx" when the bot starts. Can you help me?
You can do it with this code:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
const list = client.guilds.get("guild ID");
list.members.forEach(member => {
if (member.roles.some(role => role.name === 'Your role')) {
member.send('Your message')
}
});
});
The way it works is when the bot is ready it will check each member if they have the specified role and if they do then the bot will send it a DM.
A tip, when asking questions like this provide more information on what the problem is, what you've tried and you're current code.
I wanna use an bot to react to every single message in an channel using discord.js f.e. i got an emoji contest channel and i wanna ad an ✅ and an ✖ reaction on every post in there
ofc, all the unnecesary messages are cleaned up so that there are like 50 messages
Fetch the messages already sent in a channel with TextChannel.fetchMessages().
Iterate through the Collection.
Add reactions with Message.react().
When a new message is sent in the channel, you should also add the reactions.
const emojiChannelID = 'ChannelIDHere';
client.on('ready', async () => {
try {
const channel = client.channels.get(emojiChannelID);
if (!channel) return console.error('Invalid ID or missing channel.');
const messages = await channel.fetchMessages({ limit: 100 });
for (const [id, message] of messages) {
await message.react('✅');
await message.react('✖');
}
} catch(err) {
console.error(err);
}
});
client.on('message', async message => {
if (message.channel.id === emojiChannelID) {
try {
await message.react('✅');
await message.react('✖');
} catch(err) {
console.error(err);
}
}
});
In this code, you'll notice I'm using a for...of loop rather than Map.forEach(). The reasoning behind this is that the latter will simply call the methods and move on. This would cause any rejected promises not to be caught. I've also used async/await style rather than then() chains which could easily get messy.
According to https://discord.js.org/#/docs/main/stable/class/TextChannel
you can use fetchMessages
to get all messages from a specific channel, which then returns a collection of Message
Then you can use .react function to apply your reactions to this collection of message by iterating over it and calling .react on each.
Edit:
channelToFetch.fetchMessages()
.then(messages => {
messages.tap(message => {
message.react(`CHARACTER CODE OR EMOJI CODE`).then(() => {
// Do what ever or use async/await syntax if you don't care
about Promise handling
})
})
})