简体   繁体   English

点击这个反应表情符号DIscord.js时是否可以删除消息

[英]is there can delete message when click this reaction emoji DIscord.js

How to fix this?如何解决这个问题? i want to delete message when user click reaction X我想在用户点击反应 X 时删除消息

client.on('message', async message => {
  if (message.channel.id === emojiChannelID) {
    try {
      await message.react('✅');
      await message.react('✖');
    } catch(err) {
      console.error(err);
    }
  }
});```

There's an message.awaitReaction() in discord.js , that will return reactions from users discord.js中有一个message.awaitReaction() ,它将返回用户的反应

// Filter for only
const filter = function(reaction, user) { 
  return reaction.emoji.name === '✅' || reaction.emoji.name === '✖';
}

// {...}

let reactionMessage = await message.react('✅');
// Make sure to set max: 1 so that the promise returns after the first reaction
let reactionCollection = await reactionMessage.awaitReactions(filter, { max: 1});
// reactionCollection is a Collection<string, MessageReaction>
// Use first() to get the first (and only)
let reaction = reactionCollection.first();

Kian here,在这里,基安,

This code should work for you, if you would like I can go through and explain each line:)这段代码应该适合你,如果你愿意,我可以通过 go 并解释每一行:)

Have a good day chief!祝楼主天天开心!

async function emojiMessage(message, validReactions) {
            for (const reaction of validReactions) await message.react(reaction);
        const filter = (reaction, user) => validReactions.includes(reaction.emoji.name) && (!user.bot)

        return message
            .awaitReactions(filter, {
                max: 1,
                time: 42000
            })
            .then(collected => collected.first() && collected.first().emoji.name);
}

async function deleteMessage(message) {

    const emoji = await emojiMessage(message, ["✅", "❌"]);
    console.log(emoji)
        // if the emoji is a tick:
    if (emoji === "✅") {
        // delete their message
        console.log("tick")
        if (message.deletable == true) {
            console.log("can delete")
            console.log("attempting to delete")
            message.delete()
        }
        if (!message.deletable == false) {
            "cannot delete"
        }
    } else if (emoji === "❌") { // if the emoji is a cross
        /*
         *  do something else
         */
        return;
    }

}


client.on('message', message => {

    if (message.channel.id === emojiChannelID) {
        // runs the function
        deleteMessage(message)
    }
    /*
     * do something else
     */
})

Note:笔记:

First upload首次上传

I've tried my best to make the code understandable/work, if there is any issues feel free to comment, I'll fix it:)我已尽力使代码易于理解/工作,如果有任何问题随时发表评论,我会修复它:)

Example Usage:示例用法:

const m = await message.channel.send('hi!');
reactionDelete(m, message, 20000); // assuming 'message' is the actual sent message

async function reactionDelete (botMessage, playerMessage, timeout) {

    const filter = (reaction, user) => {
        return ['🗑️'].includes(reaction.emoji.name) && user.id === playerMessage.author.id;
    };

    botMessage.react('🗑️');

    botMessage.awaitReactions(filter, { max: 1, time: timeout})
    .then(collected => {
        const reaction = collected.first();

        if (reaction.emoji.name === '🗑️') {
            botMessage.delete();
        }
    })
    .catch(collected => {
        if (botMessage.deletable) botMessage.reactions.removeAll();
    });

};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM