下面的代码可以正常工作,但我想将其嵌入消息并在其下方显示反应。我尝试了很多方法,甚至将消息作为嵌入发布,但无法获得添加到其中的反应。有人可以帮我解决这个问题。
const firstMessage = require('./first-message')
module.exports = bot => {
const channelId = '781607965013966869'
const getEmoji = emojiName => bot.emojis.cache.find(emoji => emoji.name === emojiName)
const emojis = {
emoji: 'role',
emoji2: 'role2',
emoji3: 'role3',
emoji4: 'role4'
}
const reactions = []
let emojiText = "blah blah blah blah"
for (const key in emojis) {
const emoji = getEmoji(key)
reactions.push(emoji)
const role = emojis[key]
emojiText += `${emoji} = ${role}\n`
}
firstMessage(bot, channelId, emojiText, reactions)
const handleReaction = (reaction, user, add) => {
if (user.id === `785046361077841922`) {
return
}
const emoji = reaction._emoji.name
const {
guild
} = reaction.message
const roleName = emojis[emoji]
if (!roleName) {
return
}
const role = guild.roles.cache.find(role => role.name === roleName)
const member = guild.members.cache.find(member => member.id === user.id)
if (add) {
member.roles.add(role)
} else {
member.roles.remove(role)
}
}
bot.on('messageReactionAdd', (reaction, user) => {
if (reaction.message.channel.id === channelId) {
handleReaction(reaction, user, true)
}
})
bot.on('messageReactionRemove', (reaction, user) => {
if (reaction.message.channel.id === channelId) {
handleReaction(reaction, user, false)
}
})
}
下面的代码构成了所需的第一条消息
const addReactions = (message, reactions) => {
message.react(reactions[0])
reactions.shift()
if (reactions.length > 0) {
setTimeout(() => addReactions(message, reactions), 750)
}
}
module.exports = async (bot, id, text, reactions = []) => {
const channel = await bot.channels.fetch(id)
channel.messages.fetch().then((messages) => {
if (messages.size === 0) {
channel.send(text).then((message) => {
addReactions(message, reactions)
})
} else {
for (const message of messages) {
message[1].edit(text)
addReactions(message[1], reactions)
}
}
})
}
以下是我将其嵌入的失败尝试
const firstMessage = require('./first-message')
const Discord = require('discord.js')
module.exports = bot => {
const channelId = '781607965013966869'
const getEmoji = emojiName => bot.emojis.cache.find(emoji => emoji.name === emojiName)
const emojis = {
emoji: 'role',
emoji2: 'role2',
emoji3: 'role3',
emoji4: 'role4'
}
const reactions = []
for (const key in emojis) {
const emoji = getEmoji(key)
reactions.push(emoji)
const role = emojis[key]
}
let emojiText = new Discord.MessageEmbed()
.setColor('#ff8800')
.setTitle('Title')
.setDescription(`Blah blah blah blah`)
.addField('Consoles', `${emoji} = ${role}\n`, true)
.setFooter('Discord Server Name', 'https://fakeurl/image.png')
firstMessage(bot, channelId, emojiText, reactions)
const handleReaction = (reaction, user, add) => {
if (user.id === `785046361077841922`) {
return
}
const emoji = reaction._emoji.name
const {
guild
} = reaction.message
const roleName = emojis[emoji]
if (!roleName) {
return
}
const role = guild.roles.cache.find(role => role.name === roleName)
const member = guild.members.cache.find(member => member.id === user.id)
if (add) {
member.roles.add(role)
} else {
member.roles.remove(role)
答案 0 :(得分:0)
想通了。只需将 emojiText 放入嵌入中,然后将嵌入传递给 first-message 而不是 emojiText
const Discord = require('discord.js')
const firstMessage = require('./first-message')
module.exports = bot => {
const channelId = '786073099685593088'
const getEmoji = emojiName => bot.emojis.cache.find(emoji => emoji.name === emojiName)
const emojis = {
emoji: 'role',
emoji2: 'role2',
emoji3: 'role3',
emoji4: 'role4',
}
const reactions = []
let emojiText = '\u200B'
for (const key in emojis) {
const emoji = getEmoji(key)
reactions.push(emoji)
const role = emojis[key]
emojiText += `${emoji} = ${role}\n`
}
const roleEmbed = new Discord.MessageEmbed()
.setColor('#ff6600')
.setTitle('Blah Blah')
.setThumbnail('https://fakeurl/image.png')
.setDescription(`blah blah blah blah \n\n${emojiText})
.setFooter('Discord Server Name', 'https://fakeurl/image.png')
firstMessage(bot, channelId, roleEmbed, reactions)
const handleReaction = (reaction, user, add) => {
if (user.id === `785046361077841922`) {
return
}
const emoji = reaction._emoji.name
const {
guild
} = reaction.message
const roleName = emojis[emoji]
if (!roleName) {
return
}
const role = guild.roles.cache.find(role => role.name === roleName)
const member = guild.members.cache.find(member => member.id === user.id)
if (add) {
member.roles.add(role)
} else {
member.roles.remove(role)
}
}
bot.on('messageReactionAdd', (reaction, user) => {
if (reaction.message.channel.id === channelId) {
handleReaction(reaction, user, true)
}
})
bot.on('messageReactionRemove', (reaction, user) => {
if (reaction.message.channel.id === channelId) {
handleReaction(reaction, user, false)
}
})
}