【问题标题】:Delete Embed Message with discrd.js使用 discord.js 删除嵌入消息
【发布时间】:2020-09-07 12:29:05
【问题描述】:

好的,这就是我的问题。我正在尝试删除仅包含嵌入的消息。但是,该消息不会删除。这是我尝试过的:

const embedMsg = message.embeds.find(msg => msg.title == 'Castle League Mafia');
if(embedMsg) {
  message.delete();
  return;
}

我刚刚尝试过message.delete() 一次,players 等于 1,但这也不起作用。它会删除我的消息(带有发送嵌入命令的消息)

这是我尝试做的地方:https://pastebin.com/DbuFx8Gs

这是我的完整代码:https://pastebin.com/6EJVTBFJ

【问题讨论】:

  • 小心监听器内部的监听器。每次机器人收到消息时,您似乎都在运行.on('messageReactionAdd', ...,这可能会导致问题。请改用反应收集器,如 this SO answer 中所述。

标签: javascript discord.js


【解决方案1】:

在这里试试这个代码


// when you send the embed 
const embed = message.channel.send(Embed)

// if (blah blah blah) 
if (1 == 1) { // for testing
     embed.delete()
}

在你的情况下这应该可以工作

require('dotenv').config();
const discord = require("discord.js");
const client = new discord.Client();

const prefix = '!'
const footer = "Mafia Bot V1.0 - Spoon";

var players = 0;

client.login(process.env.BOT_TOKEN);

client.on('ready', () => {
    console.log("Bot is logged in!");
    client.user.setActivity('Mafia', {type: 'PLAYING'}).then(presence => console.log(`Activity set to ${presence.activities[0].name}`)).catch(console.error);
});

client.on('message', message => {
    
    // Start Command
	 var embedMsg;
    if (message.content === prefix + "newgame") {
        let newEmbed = new discord.MessageEmbed()
        .setTitle("Castle League Mafia")
        .setDescription("**Everything here is going to be changed**\nReact to this message to join the mafia game!\n\n6 players in total are needed to play.\n\nIf you are the mafia, you will recieve a DM. If you do not recieve a DM, you are innocent.\n\n**" + players + "/6 Players**")
        .setColor("PURPLE")
        .setTimestamp()
        .setFooter(footer);
        embedMsg = message.channel.send(newEmbed);
    }

    // Add join reaction
    if(message.author.bot) {
        if(message.embeds) {
            if(embedMsg) {
                message.react('☑️');
                return;
            }
        }
    }
    
    // When players react
    client.on("messageReactionAdd", (reaction, user) => {

        if(reaction.emoji.name === "☑️"  && !user.bot) {
            players += 1;
            // When players join
            switch (players) {
                case 1:
                    console.log(players + " players are ready to play!");

                    if(embedMsg) {
                        embedMsg.delete();
                        return;
                    }

                    let startEmbed = new discord.MessageEmbed()
                    .setTitle("Game has started!")
                    .setDescription("**Mafia has been messaged!**")
                    .setColor("PURPLE")
                    .setTimestamp()
                    .setFooter(footer);
                    message.channel.send(startEmbed);
                    break;
                case 2:
                    console.log(players + " players are ready to play!");
                    break;
                case 3: 
                    console.log(players + " players are ready to play!");
                    break;
                default:
                    break;
            }
            return;
        }
    });

    client.on("messageReactionAdd", (reaction, user) => {
        
        
        if(reaction.emoji.name === "?") {

        }
    });

    client.on("messageReactionRemove", (reaction, user) => {

        if(reaction.emoji.name === "☑️"  && !user.bot) {
            players -= 1;
            console.log(players + " players are ready to play!");
            return;
        }
    })

    

});

【讨论】:

  • 嘿!感谢您尝试提供帮助。我刚刚尝试了你的建议,我得到了错误:TypeError: embedMsg.delete is not a function。在我更改代码之前我没有收到错误
猜你喜欢
  • 1970-01-01
  • 2021-10-12
  • 2018-08-26
  • 2020-07-04
  • 1970-01-01
  • 1970-01-01
  • 2021-06-12
  • 2020-07-15
  • 2021-01-09
相关资源
最近更新 更多