如何在 discordjs-commando 中编辑/替换嵌入
Posted
技术标签:
【中文标题】如何在 discordjs-commando 中编辑/替换嵌入【英文标题】:How to edit/replace embeds in discordjs-commando 【发布时间】:2021-08-23 23:23:51 【问题描述】:我正在用我的 discord 机器人制作一个 Type 赛车迷你游戏,代码可以工作......但我想更改它发送到嵌入的消息,我是 Commando 的新手,它不会让我使用我使用的 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 $counters!`
,
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】:首先更改嵌入内容与discord.js-commando
无关,以更改发送的嵌入消息的内容,您需要获取Message Object,然后使用edit()
方法将新的嵌入内容传递给它:
-Bonus:您还可以将短信编辑为嵌入消息。
编辑方法的文档: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);
【讨论】:
你是如何使用它的,我可以确定它可以工作:D以上是关于如何在 discordjs-commando 中编辑/替换嵌入的主要内容,如果未能解决你的问题,请参考以下文章
怎么在windows中编iphone\itouch程序。最好能是win7环境
Makefile,在 src 目录树中查找源代码并在构建文件中编译为 .o