我正在制作一个票务机器人,它在启动时会发送一条消息,但是一旦发送完消息,对它的反应就什么也没有发生,直到我在不和谐的服务器中完成了诸如发送消息之类的事情。我正在使用Discord.JS。
我想知道是否有任何可能的方式来缓存消息或其他内容,这样我就不必与服务器进行交互来进行反应事件以获取我的反应。
我的启动代码:
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);
})
});
});
我的反应事件:
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.');
}
}
}))
});
干杯。