I am making a ticket bot and it sends a message upon startup, but once the message has been sent, nothing happens when I react to it until I've done something like send a message in the discord server. I am using Discord.JS.
I want to know if there is any possible way to cache the message or something so that I do not have to interact with the server for the reaction event to pick up my reaction.
My Startup Code:
client.on('ready', () => {
console.log(`[AUTH] Authenticated with discord!`);
client.guilds.cache.forEach(async function(dguild) {
let channel1 = dguild.channels.cache.find(channel => channel.name === 'new-ticket')
if(!channel1) {
await dguild.createChannel("actions", { type: 'text' })
channel1 = dguild.channels.cache.find(channel => channel.name === 'new-ticket')
}
const fetched1 = await channel1.messages.fetch({ limit: 100 });
channel1.bulkDelete(fetched1);
let departments1 = "";
departments(deps => deps.forEach(dep => departments1 += '``'+dep.reactionEmoji +' '+ dep.name + '`` :: ' + dep.description + '\n\n'));
let embed1 = new Discord.MessageEmbed()
.setTitle(nte.title)
.setColor(nte.color)
.addField("\u200B", nte.header)
.addField("Departments: \n", departments1)
.addField("\u200B", nte.footer);
channel1.send(embed1).then(async m => {
await departments(deps => deps.forEach(dep => m.react(dep.reactionEmoji)));
console.log(m.id +":"+channel1.id);
dguild.channels.cache.get(channel1.id).message.cache.get(m.id);
})
});
});
My Reaction Event:
client.on('messageReactionAdd', async (reaction, user) => {
if(user.id == client.user.id) return;
const userReactions = reaction.message.reactions.cache.filter(reaction => reaction.users.cache.has(user.id));
try {
for (const reaction of userReactions.values()) {
await reaction.users.remove(user.id);
}
} catch (error) {
console.error('Failed to remove reactions.');
}
departments(deps => deps.forEach(dep => {
if (reaction.emoji.name == dep.reactionEmoji) {
try {
ticket.create(user.id, dep.id, reaction.message);
console.log(dep.name + ": Ticket Opened");
} catch {
console.log('Failed to generate ticket.');
}
}
}))
});
Cheers.