DJS - 当有人对特定表情符号做出反应时编辑嵌入
DJS - Edit the embed when someone reacts with specific emoji
我想做的是在有人添加反应时更改嵌入的颜色。
我目前面临的困难是让它成为 if 语句。所以还没走多远。
我也可能没有针对任何特定的嵌入。由于嵌入 (suggestionsEmbed) 已发送多次。
编辑:更新为当前代码。除了一件事,这是有效的。除了最后发送的以外,不能编辑任何嵌入。明白我的意思here.
// SUGGESTIONS EMBED - BEGINS
var suggestionsEmbed;
client.on('messageCreate', async message => {
if (message.channel.id === channelSuggestions && !message.author.bot) {
db.add('counterP.plus', 1);
const embed = new MessageEmbed()
.setColor('#f9db73')
.setTitle(`Suggestion #${await db.fetch('counterP.plus') + await db.fetch('counterM.minus')}`)
.setDescription(message.content)
.setAuthor({ name: message.author.username, iconURL: message.author.displayAvatarURL() })
.setTimestamp()
.setFooter({ text: message.author.id });
suggestionsEmbed = await message.channel.send({ embeds: [embed] })
suggestionsEmbed.react('')
suggestionsEmbed.react('');
message.delete({ timeout: 0 });
console.log(db.fetch('counterP.plus'));
}
})
// SUGGESTIONS EMBED - ENDS
// CHANGE EMBED COLOR - BEGINS
const suggestionsApproved = '';
const suggestionsDenied = '';
client.on('messageReactionAdd', (reaction, user) => {
if (reaction.message.channel.id === channelSuggestions) {
if (reaction.emoji.name === suggestionsApproved && reaction.message.id === suggestionsEmbed.id) {
const suggestionsEmbedApproved = new MessageEmbed(suggestionsEmbed.embeds[0]).setColor('#76de51')
suggestionsEmbed.edit({ embeds: [suggestionsEmbedApproved] });
}
}
})
所以您的代码正在尝试编辑嵌入而不是嵌入所在的消息,(我知道这很奇怪)但这应该适合您。
Discord 意图可能因您的编码而异,但看起来应该相似
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MESSAGE_REACTIONS],
partials: ['MESSAGE', 'CHANNEL', 'REACTION'],
})
它应该能够通过以下方式获取这些反应。
const suggestionsApproved = 'insertReactionEmojiName'
const suggestionsDenied = 'insertReactionEmojiName'
const channelSuggestions = 'yaddayadda'
// I would use a different emoji, one that you can get an name from like this
// which in discord is named :thumbsup:
client.on('messageReactionAdd', (reaction, user) => {
if (reaction.partial) {
// If the message this reaction belongs to was removed, the fetching might result in an API error which should be handled
try {
await reaction.fetch();
} catch (error) {
console.error('Something went wrong when fetching the message:', error);
// Return as `reaction.message.author` may be undefined/null
return;
}
}
const targetMessage = reaction.message
const channel = targetMessage.channel
if (channel.id === channelSuggestions) {
if (reaction.emoji.name === suggestionsApproved) {
const suggestionsEmbedApproved = new MessageEmbed()
.setColor('#1dce1d')
targetMessage.edit({
embeds: [suggestionsEmbedApproved]
})
console.log('fired!')
}
}
})
引入 discord.js v13 后,您需要 intents 来接收公会中发生的事件。您可以在 => Gateway Intents 中了解更多关于 Intent 的信息。首先,使用以下方法在您的客户端中添加 GUILD_MESSAGE_REACTIONS
意图:
const { Client, Intents } = require('discord.js')
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS
]
})
之后,你就可以用你原来的代码做任何你想做的事了。虽然您可能会收到一条错误提示 DiscordAPIError: Invalid Form Body embeds[0].description: This field is required
,但要解决此问题,您只需将 .setDescription()
添加到您编辑的嵌入中,然后发送即可。您的最终代码可能如下所示:
const { Client, Intents, MessageEmbed } = require('discord.js')
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS
]
})
const suggestionsApproved = '';
const suggestionsDenied = '';
const channelSuggestions = 'your channel id'
client.on('messageReactionAdd', (reaction, user) => {
if (reaction.message.channel.id === channelSuggestions) {
if (reaction.emoji.name === suggestionsApproved) {
const suggestionsEmbedApproved = new MessageEmbed()
.setColor('your color')
.setDescription('your description');
suggestionsEmbed.edit({ embeds: [suggestionsEmbedApproved]});
}
}
})
编辑
针对新问题,答案如下:
不要将嵌入命名为 suggestionsEmbed
,正确的做法是首先创建一个具有不同名称的嵌入,然后使用 const suggestionsEmbed = await message.channel.send({ embeds: [embedName] })
,这样代码将类似于:
const embed = new MessageEmbed()
.setTitle('title')
.setDescription('description')
.setColor('color')
const suggestionsEmbed = await message.channel.send({
embeds: [embed]
})
第二次编辑
由于您已经在使用 await message.channel.send()
,因此您不必使用 .then()
。你所要做的就是改变这个:
suggestionsEmbed = await message.channel.send({ embeds: [embed] }).then(sentEmbed => {
sentEmbed.react("")
sentEmbed.react("")
});
对此:
suggestionsEmbed = await message.channel.send({ embeds: [embed] })
suggestionsEmbed.react('')
suggestionsEmbed.react('')
编辑嵌入:
使用:
const suggestionsEmbedApproved = new MessageEmbed(suggestionsEmbed.embeds[0]).setTitle('asdfasdf')
suggestionsEmbed.edit({ embeds: [suggestionsEmbedApproved] });
要编辑任何有反应的嵌入,而不仅仅是前一个发送的嵌入,您可以使用:
const suggestionsEmbedApproved = new MessageEmbed(reaction.message.embeds[0]).setColor("Your color")
reaction.message.edit({ embeds: [suggestionsEmbedApproved] });
我想做的是在有人添加反应时更改嵌入的颜色。 我目前面临的困难是让它成为 if 语句。所以还没走多远。
我也可能没有针对任何特定的嵌入。由于嵌入 (suggestionsEmbed) 已发送多次。
编辑:更新为当前代码。除了一件事,这是有效的。除了最后发送的以外,不能编辑任何嵌入。明白我的意思here.
// SUGGESTIONS EMBED - BEGINS
var suggestionsEmbed;
client.on('messageCreate', async message => {
if (message.channel.id === channelSuggestions && !message.author.bot) {
db.add('counterP.plus', 1);
const embed = new MessageEmbed()
.setColor('#f9db73')
.setTitle(`Suggestion #${await db.fetch('counterP.plus') + await db.fetch('counterM.minus')}`)
.setDescription(message.content)
.setAuthor({ name: message.author.username, iconURL: message.author.displayAvatarURL() })
.setTimestamp()
.setFooter({ text: message.author.id });
suggestionsEmbed = await message.channel.send({ embeds: [embed] })
suggestionsEmbed.react('')
suggestionsEmbed.react('');
message.delete({ timeout: 0 });
console.log(db.fetch('counterP.plus'));
}
})
// SUGGESTIONS EMBED - ENDS
// CHANGE EMBED COLOR - BEGINS
const suggestionsApproved = '';
const suggestionsDenied = '';
client.on('messageReactionAdd', (reaction, user) => {
if (reaction.message.channel.id === channelSuggestions) {
if (reaction.emoji.name === suggestionsApproved && reaction.message.id === suggestionsEmbed.id) {
const suggestionsEmbedApproved = new MessageEmbed(suggestionsEmbed.embeds[0]).setColor('#76de51')
suggestionsEmbed.edit({ embeds: [suggestionsEmbedApproved] });
}
}
})
所以您的代码正在尝试编辑嵌入而不是嵌入所在的消息,(我知道这很奇怪)但这应该适合您。
Discord 意图可能因您的编码而异,但看起来应该相似
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MESSAGE_REACTIONS],
partials: ['MESSAGE', 'CHANNEL', 'REACTION'],
})
它应该能够通过以下方式获取这些反应。
const suggestionsApproved = 'insertReactionEmojiName'
const suggestionsDenied = 'insertReactionEmojiName'
const channelSuggestions = 'yaddayadda'
// I would use a different emoji, one that you can get an name from like this
// which in discord is named :thumbsup:
client.on('messageReactionAdd', (reaction, user) => {
if (reaction.partial) {
// If the message this reaction belongs to was removed, the fetching might result in an API error which should be handled
try {
await reaction.fetch();
} catch (error) {
console.error('Something went wrong when fetching the message:', error);
// Return as `reaction.message.author` may be undefined/null
return;
}
}
const targetMessage = reaction.message
const channel = targetMessage.channel
if (channel.id === channelSuggestions) {
if (reaction.emoji.name === suggestionsApproved) {
const suggestionsEmbedApproved = new MessageEmbed()
.setColor('#1dce1d')
targetMessage.edit({
embeds: [suggestionsEmbedApproved]
})
console.log('fired!')
}
}
})
引入 discord.js v13 后,您需要 intents 来接收公会中发生的事件。您可以在 => Gateway Intents 中了解更多关于 Intent 的信息。首先,使用以下方法在您的客户端中添加 GUILD_MESSAGE_REACTIONS
意图:
const { Client, Intents } = require('discord.js')
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS
]
})
之后,你就可以用你原来的代码做任何你想做的事了。虽然您可能会收到一条错误提示 DiscordAPIError: Invalid Form Body embeds[0].description: This field is required
,但要解决此问题,您只需将 .setDescription()
添加到您编辑的嵌入中,然后发送即可。您的最终代码可能如下所示:
const { Client, Intents, MessageEmbed } = require('discord.js')
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS
]
})
const suggestionsApproved = '';
const suggestionsDenied = '';
const channelSuggestions = 'your channel id'
client.on('messageReactionAdd', (reaction, user) => {
if (reaction.message.channel.id === channelSuggestions) {
if (reaction.emoji.name === suggestionsApproved) {
const suggestionsEmbedApproved = new MessageEmbed()
.setColor('your color')
.setDescription('your description');
suggestionsEmbed.edit({ embeds: [suggestionsEmbedApproved]});
}
}
})
编辑
针对新问题,答案如下:
不要将嵌入命名为 suggestionsEmbed
,正确的做法是首先创建一个具有不同名称的嵌入,然后使用 const suggestionsEmbed = await message.channel.send({ embeds: [embedName] })
,这样代码将类似于:
const embed = new MessageEmbed()
.setTitle('title')
.setDescription('description')
.setColor('color')
const suggestionsEmbed = await message.channel.send({
embeds: [embed]
})
第二次编辑
由于您已经在使用 await message.channel.send()
,因此您不必使用 .then()
。你所要做的就是改变这个:
suggestionsEmbed = await message.channel.send({ embeds: [embed] }).then(sentEmbed => {
sentEmbed.react("")
sentEmbed.react("")
});
suggestionsEmbed = await message.channel.send({ embeds: [embed] })
suggestionsEmbed.react('')
suggestionsEmbed.react('')
const suggestionsEmbedApproved = new MessageEmbed(suggestionsEmbed.embeds[0]).setTitle('asdfasdf')
suggestionsEmbed.edit({ embeds: [suggestionsEmbedApproved] });
要编辑任何有反应的嵌入,而不仅仅是前一个发送的嵌入,您可以使用:
const suggestionsEmbedApproved = new MessageEmbed(reaction.message.embeds[0]).setColor("Your color")
reaction.message.edit({ embeds: [suggestionsEmbedApproved] });