如何在 discordjs-commando 中编辑/替换嵌入

时间:2021-06-06 17:52:39

标签: node.js discord discord.js commando

我正在用我的 discord bot 制作一个 Type 赛车迷你游戏,代码有效......但我想更改它发送到嵌入的消息,我是突击队的新手,它不会让我使用我使用的 discord.js 函数使用

我需要将所有机器人响应更改为嵌入,并使其在发送新嵌入时只编辑旧嵌入,以免发送垃圾邮件。这是我的代码:

const Commando = require('discord.js-commando')
const { words } = require('../../util/fast-type-words')

const example = {
  channelId: {
    message: 'message object',
    stage: 'string',
    counter: 'number',
    currentWord: 'string',
    remainingWords: ['words here'],
    points: {
      userId: 'points',
    },
  },
}

const games = {}

const stages = {
  STARTING: (counter) => {
    return `A new "fast type" game is starting in ${counter}s!`
  },
  IN_GAME: (word) => {
    let spacedWord = ''

    for (const character of [...word]) {
      spacedWord += character
      spacedWord += ' '
    }

    return `The new word is **${spacedWord}**!`
  },
  ENDING: (points) => {
    const sorted = Object.keys(points).sort((a, b) => {
      return points[b] - points[a]
    })

    let results = ''

    for (const key of sorted) {
      const amount = points[key]
      results += `<@${key}> had ${amount} point${amount === 1 ? '' : 's'}\n`
    }

    return `The game is now over Here's how everyone did:\n\n${results}------------------`
  },
}

const selectWord = (game) => {
  game.currentWord =
    game.remainingWords[Math.floor(Math.random() * game.remainingWords.length)]

  const index = game.remainingWords.indexOf(game.currentWord)
  game.remainingWords.splice(index, 1)
}

const gameLoop = () => {
  for (const key in games) {
    const game = games[key]
    const { message, stage } = game

    if (stage === 'STARTING') {
      let string = stages[stage](game.counter)
      message.edit(string)

      if (game.counter <= 0) {
        game.stage = 'IN_GAME'
        game.counter = 15

        selectWord(game)

        string = stages[game.stage](game.currentWord)
        message.edit(string)
      }
    } else if (stage === 'IN_GAME') {
      if (game.counter <= 0) {
        game.stage = 'ENDING'

        const string = stages[game.stage](game.points)
        message.edit(string)

        // Delete the game
        delete games[key]

        continue
      }
    }

    --game.counter
  }

  setTimeout(gameLoop, 1000)
}

module.exports = class FastTypeGame extends Commando.Command {
  constructor(client) {
    super(client, {
      name: 'fasttype',
      group: 'games',
      memberName: 'fasttype',
      description: 'Starts a fast type game',
      userPermissions: ['ADMINISTRATOR'],
    })

    client.on('message', (message) => {
      const { channel, content, member } = message
      const { id } = channel

      const game = games[id]

      if (game && game.currentWord && !member.user.bot) {
        message.delete()

        if (
          game.stage === 'IN_GAME' &&
          content.toLowerCase() === game.currentWord.toLowerCase()
        ) {
          game.currentWord = null
          const seconds = 2

          const { points } = game
          points[member.id] = points[member.id] || 0

          message
            .reply(`You won! +1 point (${++points[member.id]} total)`)
            .then((newMessage) => {
              newMessage.delete({
                timeout: 1000 * seconds,
              })
            })

          setTimeout(() => {
            if (game.stage === 'IN_GAME') {
              selectWord(game)

              const string = stages[game.stage](game.currentWord)
              game.message.edit(string)
            }
          }, 1000 * seconds)
        }
      }
    })

    gameLoop()
  }

  async run(message) {
    const { channel } = message

    message.delete()
    channel.send('Preparing game...').then((message) => {
      games[channel.id] = {
        message,
        stage: 'STARTING',
        counter: 5,
        remainingWords: [...words],
        points: {
          '719805930547445772': 4,
          '723819104045105172': 1,
        },
      }
    })
  }
}

1 个答案:

答案 0 :(得分:0)

首先更改嵌入内容与discord.js-commando无关,要更改发送的嵌入消息的内容,您需要获取Message Object,然后使用edit()方法将新的嵌入内容传递给它:

-奖励:您还可以将文本消息编辑为嵌入消息。

编辑方法的文档:https://discord.js.org/#/docs/main/stable/class/Message?scrollTo=edit

示例代码:


let youCurrentMessage = await channel.send(embedContent);

yourCurrentMessage.edit(newEmbedContent);
yourCurrentMessage.edit(newEmbedContent2);

// If you edit message in other command , session.You need message id

let yourCurrentMessage = await msg.channel.messages.fetch(editMessageId);
yourCurrentMessage.edit(newEmbedContent);