首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何编辑/替换discordjs-commando中的嵌入

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

Stack Overflow用户
提问于 2021-06-07 01:52:39
回答 1查看 205关注 0票数 0

我正在用我的不和谐机器人做一个类型赛车小游戏,代码工作…但是我想改变它发送给embeds的消息,我是特种部队的新手,它不让我使用im习惯使用的discord.js函数。

我需要改变所有的机器人响应嵌入,并使它,当它发送一个新的嵌入,它只是编辑旧的,所以它不是垃圾邮件。下面是我的代码:

代码语言:javascript
复制
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,
        },
      }
    })
  }
}

EN

回答 1

Stack Overflow用户

发布于 2021-06-07 02:45:16

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

-Bonus:你也可以编辑文本信息到嵌入信息中。

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

示例代码:

代码语言:javascript
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67862083

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档