【问题标题】:How to have Discord bot edit embed message in this case?在这种情况下如何让 Discord bot 编辑嵌入消息?
【发布时间】:2021-03-23 11:07:09
【问题描述】:

现在,我有一个 Discord 机器人,可以为游戏设置“注册”。

我正在寻找的是让用户能够发送消息“+join (class)”来注册一个插槽。因此,如果我说“+join Soldier”,它会将我的名字放在插槽中,如下所示:

如果所有名额都已满或工作人员键入“+close”,它将关闭当前游戏的注册。我知道我必须使用 .edit 函数将嵌入替换为相同的新嵌入,但用户名已放入插槽中,但我真的不知道该怎么做,特别是因为这涉及两个单独的 javascript 文件。我真的很感激所有的帮助。太感谢了! :)

NewGameCommand.js:

const Discord = require('discord.js');
module.exports = class NewgameCommand extends BaseCommand {
  constructor() {
    super('newgame', 'managment', []);
  }

  async run(client, message, args) {
    if (!message.member.hasPermission("MANAGE_ROLES")) {
      return message.channel.send("Only staff members can set up games!");
    }
    let gameEmbed = new Discord.MessageEmbed()
      .setTitle('')
      .setDescription('')
      .setColor("#FF0000")
    const twoCitiesMissionsDisplayed = "\`\`\`\n1 - ????Empire Escalation\n2 - ????Metro Malice\n3 - ????Hamlet Hostility\n4 - ????Bavarian Botbash\`\`\`";
    const twoCitiesMissionsArray = ['????Empire Escalation', '????Metro Malice', '????Hamlet Hostility', '????Bavarian Botbash'];
    const numOfMissions = ['1', '2', '3', '4'];
    const cancelWords = ['cancel', 'quit', 'stop', '+cancel', '+quit', '+stop'];
    const mvmChannel = client.channels.cache.get('787130524703129611');
    let classMapDefault = new Map();
    classMapDefault['Soldier'] = '';
    classMapDefault['Demo/Pyro'] = '';
    classMapDefault['Heavy'] = '';
    classMapDefault['Medic'] = '';
    classMapDefault['Engineer'] = '';
    classMapDefault['Scout'] = '';
   
    const filter = m => m.author.id === message.author.id;
    message.reply("which   Two Cities mission?");
    message.channel.send(twoCitiesMissionsDisplayed);
    message.channel.awaitMessages(filter, {max: 1, time: 30000}).then(collected => {
      if(cancelWords.includes(collected.first().content)) {
        return message.channel.send("Canceled.");
      }
      let missionNumString = collected.first().content;
      // if they don't choose 1, 2, 3 or 4
      if(!numOfMissions.includes(missionNumString)) {
        return message.channel.send("Invalid mission number!")
      }  
      let missionNum = parseInt(missionNumString) - 1;
      let missionName = twoCitiesMissionsArray[missionNum];
      message.channel.send(`Great, we'll be playing the ${missionName} mission this week! Are we playing with the default class set? (yes/no)`);
      message.channel.awaitMessages(filter, {max: 1, time: 30000}).then(collected => {
        if(cancelWords.includes(collected.first().content)) {
          return message.channel.send("Canceled.");
        }
        let classResponseArray = collected.first().content;
        let classResponse = classResponseArray[0].toLowerCase();
        if (classResponse.charAt(0) === 'y') {
          let classMap = classMapDefault;
          const class1 = 'Soldier';
          const class2 = 'Demo/Pyro';
          const class3 = 'Heavy';
          const class4 = 'Medic';
          const class5 = 'Engineer';
          const class6 = 'Scout';
          message.channel.send('Got it! What time? (PST)\nFormat it like \`6:30\` or \`7:00\`');
          message.channel.awaitMessages(filter, {max: 1, time: 30000}).then(collected => {
            if(cancelWords.includes(collected.first().content)) {
              return message.channel.send("Canceled.");
            }
            let timeResponse = collected.first().content;
            gameEmbed.setTitle(`New MvM Mondays Game! This Monday @ ${timeResponse} PST.`);
            let gameDescription = `Tour: ????️Two Cities\nMission: ${missionName}\n`
            let class1description = `${class1}: open⭕\n`
            let class2description = `${class2}: open⭕\n`
            let class3description = `${class3}: open⭕\n`
            let class4description = `${class4}: open⭕\n`
            let class5description = `${class5}: open⭕\n`
            let class6description = `${class6}: open⭕\n`
            gameDescription = gameDescription + '\`\`\`' + class1description + class2description + class3description + class4description + class5description + class6description + "\`\`\`";
            gameEmbed.setDescription(gameDescription);
            let openOrClosed = `OPEN????`;
            gameEmbed.setFooter(`Sign-ups: ${openOrClosed}`);
            message.channel.send(`${timeResponse} it is! Here's what will be sent to ${mvmChannel}:`);
            message.channel.send(gameEmbed);
            message.channel.send('Ready to send? Type \"yes\" to send or \"no\" to cancel.');
            message.channel.awaitMessages(filter, {max: 1, time: 30000}).then(collected => {
              if(cancelWords.includes(collected.first().content)) {
                return message.channel.send("Canceled.");
              }
              let sendResponseArray = collected.first().content;
              let sendResponse = sendResponseArray[0].toLowerCase();
              if (sendResponse.charAt(0) === 'y') {
                mvmChannel.send(gameEmbed);
                message.send(`MvM Mondays Sign-up created in ${mvmChannel}!`);
              } else if (sendResponse.charAt(0) === 'n') {
                message.channel.send("Canceled.");
              } else {
                return message.reply('you just had to say \"yes\" or \"no\"!');
              }      
            }).catch(err => {
              console.log(err)
            })
          }).catch(err => {
            console.log(err)
          })
        } else if (classResponse.charAt(0) === 'n') {
          return message.channel.send('I only know the default class sets right now...');
        } else {
          return message.reply('you just had to say \"yes\" or \"no\"!');
        }
      }).catch(err => {
        console.log(err)
      })
    }).catch(err => {
      console.log(err)
    })  
  }
}

JoinCommand.js:

const BaseCommand = require('../../utils/structures/BaseCommand');
const Discord = require('discord.js');

module.exports = class JoinCommand extends BaseCommand {
  constructor() {
    super('join', 'member', []);
  }

  run(client, message, args) {
    const mvmClass = args[0];
    message.channel.send(message.author.toString() + ` joined as ${mvmClass}!`);
  }
}

【问题讨论】:

    标签: javascript string discord discord.js


    【解决方案1】:

    您在https://discord.js.org 页面上使用<Message>.edit()

    例子:

    let text = "Hello world!";
    message.channel.send(text).then(msg=> {
        text = text.replace("world", "my friend") // "Hello my friend!"
        msg.edit(text)
    }
    

    let msg = message.channel.send("Hello world");
    msg.edit("Hello my friend");
    

    请再次阅读文档!或者只是先谷歌它。

    【讨论】:

    • 对。我想我更想知道如何在一个 js 文件中创建一条消息并在另一个 js 文件中引用它。
    • 我不明白你的意思。你的意思是像在一个 js 文件中创建一个嵌入,然后在另一个 js 文件中发送它?
    • 基本上,是的。在一个 js 文件中创建并发送它并在另一个 js 文件中编辑它。我想我可以将 messageID 存储在 json 中并通过引用 messageID 来编辑消息。
    • 是的,你可以获取消息然后编辑它,client.guilds.cache.get("GUILD_ID").channels.get("CHANNEL_ID").messages.fetch("MESSAGE_ID").then((m) => {}) 之后你只需使用m.edit("YOUR_TEXT")
    猜你喜欢
    • 2021-08-12
    • 1970-01-01
    • 2022-01-05
    • 2021-08-01
    • 2020-05-11
    • 2019-03-23
    • 2021-12-12
    • 2019-10-07
    • 2021-05-04
    相关资源
    最近更新 更多