discord.js - 嵌入的角色声明消息
问题描述
下面的代码没有任何问题,但我想让它成为一个嵌入的消息,下面有反应。我尝试了很多方法,甚至将消息作为嵌入发布,但无法获得添加到其中的反应。有人可以帮我解决这个问题。
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)
解决方案
弄清楚了。只需将 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)
}
})
}
推荐阅读
- database - Doctrine ORM - 同一对象之间的关系
- javascript - 限制输入到文本框:它只接受数字,不应该接受十进制值
- javascript - 如何使用django递归关系回复特定评论并反应js
- python - 如何根据另一列值填充空索引或空行?
- java - 当类存储在数组中时Java返回数据类型
- php - Laravel 自定义 websocket 处理程序不起作用
- android - 我将 Android Studio 更新到最新版本(3.6.1),现在找不到 Gradle 插件
- keras - 无效参数:indices[0,0] = -4 不在 [0, 40405)
- json - JSON Schema 验证具有不同属性名称的 JSON
- linux - 如何在 Linux BASH 上使用 split 在文件末尾添加编号后缀?