首页 > 解决方案 > 尝试在 react emoji 上做 discord.js 得到 dm

问题描述

当您对一个或两个表情符号做出反应时,我如何制作一个向您发送 dm 的机器人?我不擅长 JavaScript,所以我请求你的帮助。

bot.on('message', message => {
    if(message.author.bot)
    {
        if(message.embeds)
        {
            const embedMsg = message.embeds.find(msg => msg.title === 'server roles');
            if(embedMsg)
            {
                embedMsg.message.react('706754740767424572')
                .then(reaction => reaction.message.react('706761878721789982'))
                .then(msg => console.log("Deleted message"))
                .catch(err => console.error);
            }
        }
        return;
    }
    if(message.content.toLowerCase() === '!roles')
    {
        const embed = new Discord.RichEmbed();
        embed.setTitle("server roles");
        embed.setColor("BLUE")
        embed.setDescription("<:Patreon:706754740767424572> - patreonas")
        message.channel.send(embed);
    }
});


bot.on('messageReactionAdd', (reaction, user) => {
    if(reaction.message.id !== '737968410377453578') return
    user.send('...')
    .catch(console.error)
  });

标签: javascriptdiscorddiscord.js

解决方案


在机器人事件 messageReactionAdd 中,您需要添加:

let msgId = [];

client.on("message", (message) => {
    if(message.content === "command") {
        message.channel.send(
            new discord.MessageEmbed()
                .setAuthor("embed")
                .setDescription("a sample descrption")
        ).then(message => {
            msgId.push(message.id);
            message.react("");
        })
    }
})

client.on('messageReactionAdd', (reaction, user) => {
    if(user.id !== bot.user.id) {
        if(reaction.message.id === msgId[0]) {
            user.send("the message sent to the person that had react");
        }
    }
});

该代码的目标是在发送特定消息时发送消息嵌入,然后机器人将新的嵌入 id 推送到数组并向此嵌入添加反应。

那么当有人对此反应做出反应时,如果此反应的作者不是机器人,那么机器人会向对嵌入的消息做出反应的人发送消息。

此外,您应该使用以下方法将 discord.js 库更新为 discord.js v12npm update discord.js


推荐阅读