所以我有这个discord.js机器人,它发送嵌入消息并根据命令向其描述添加一个值,并在5秒后删除该值,但每次我向描述中添加一个值时,当它已经有一个值时,当第一个值在 5 秒后被删除时,我添加的值就会消失。
这是我的代码:
const { Client, Intents, MessageEmbed } = require('discord.js')
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] })
const mongoose = require('mongoose')
mongoose.connect(/*db adress*/, {useNewUrlParser: true, useUnifiedTopology: true})
const Schema = mongoose.Schema
const embedSchema = new Schema({
exists: { type: String, required: true },
embedId: { type: String, required: true },
channelId: { type: String, required: true }
})
const Embed = mongoose.model('Embed', embedSchema, 'embed')
const embedMessage = new MessageEmbed()
.setDescription('empty')
client.on('messageCreate', async message => {
if (message.content === '.sendEmbed') {
const embed = await message.channel.send({ embeds: [embedMessage] })
const saveEmbed = new Embed({
exists: 'true',
embedId: embed.id,
channelId: message.channel.id
})
saveEmbed.save()
} else if (message.content.startsWith('.add')) {
const wordsInMessage = message.content.split(' ')
if (!wordsInMessage[2]) {
Embed.find({ exists: 'true' }, async (err, embed) => {
const word = wordsInMessage[1]
const oldEmbed = await message.channel.messages.fetch(embed[0].embedId)
const newEmbed = oldEmbed.embeds[0]
const embedDescription = newEmbed.description.replaceAll('empty', '')
newEmbed.setDescription(`${embedDescription}\n${word}`)
oldEmbed.edit({ embeds: [newEmbed] })
setTimeout(() => updateValue(word), 5000)
})
}
}
})
function updateValue(word) {
Embed.find({ exists: 'true' }, async (err, embed) => {
Array.prototype.replaceAt = function(index, replacement) {
if (index >= this.length) {
return this.valueOf()
}
return this.splice(0, index) + replacement + this.splice(index + 1)
}
const channel = client.channels.cache.get(embed[0].channelId)
const getEmbed = await channel.messages.fetch(embed[0].embedId)
const embedMessage = await getEmbed.embeds[0]
const wordsPosition = embedMessage.description.split('\n').indexOf(word)
const newValue = embedMessage.description.split('\n')[wordsPosition].split(' ').replaceAt(0, 'empty')
embedMessage.setDescription(`${newValue}`)
getEmbed.edit({ embeds: [embedMessage] })
})
}
client.login(/*bot token*/)
const { MessageEmbed } = require("discord.js");
const Schema = require("path-to-schema");
const data = await Schema.find({ exists: true });
const msg = await message.channel.messages.fetch(data.embedId);
if(!msg && !msg.embeds) return;
const oldEmbed = msg.embeds[0];
const newEmbed = new MessageEmbed(oldEmbed.toJSON()).setDescription("new description with existing embed details");
await msg.edit({ embeds: [newEmbed] });
所以上面的代码基本上是从你的
Schema
中获取消息ID并获取当前频道中的消息。它使用现有嵌入的 JSON 创建一个新嵌入并更新描述,最终更新旧消息。
这可以通过 Spread 语法实现 (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax)。
alliances = ["test1","test2","test3","test4"]
let dict_alliances = []
alliances.forEach(alliance =>{
dict_alliances.push({ name: alliance, value: alliance })
})
module.exports = {
name: "post",
data: new SlashCommandBuilder()
.setName('post')
.setDescription('desc')
.addSubcommand(subcommand =>
subcommand
.setName('name')
.setDescription('desc')
.addStringOption(option => option.setName('alliance')
.setDescription("desc")
.setRequired(true)
.addChoices(...dict_alliances))