所以几周前我开始学习 node.js 和 javascript,在过去的几天里,我试图在不和谐的情况下制作机器人,我想制作一个能够做多种事情的机器人,其中之一是给予角色,我遵循了 youtube 教程的每一步,但仍然不能完全工作,这是我运行的 index.js
const client = new Discord.Client()
const config = require('./config.json')
const command = require('./command')
const roleClaim = require('./role-claim')
client.on('ready', () => {
console.log('The client is ready!')
})
roleClaim(client)
});
client.login(config.token)
配置和命令我 100% 确定它们有效,我认为真正的问题在于角色声明
const firstMessage = require('./first-message')
module.exports = (client) => {
const channelId = 'TheIdOfTheChannel' #in the code i actually write the id of the channel
const getEmoji = (emojiName) =>
client.emojis.cache.find((emoji) => emoji.name === emojiName)
const emojis = {
civilian: 'civilian', #civilian is a customized emoji in my server
studento: 'student' #studento is a customized emoji in my server too
}
const reactions = []
let emojiText = 'Add a reaction to claim a role\n\n'
for (const key in emojis) {
const emoji = getEmoji(key)
reactions.push(emoji)
const role = emojis[key]
emojiText += `${emoji} = ${role}\n`
}
firstMessage(client, channelId, emojiText, reactions)
const handleReaction = (reaction, user, add) => {
if (user.id === 'IdOfTheBot') { #in the code i actually write the id of the bot
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)
}
}
client.on('messageReactionAdd', (reaction, user) => {
if (reaction.message.channel.id === channelId) {
handleReaction(reaction, user, true)
}
})
client.on('messageReactionRemove', (reaction, user) => {
if (reaction.message.channel.id === channelId) {
handleReaction(reaction, user, false)
}
})
}
first-message 使机器人在运行时执行 role-claim.js 代码,该代码的一部分实际有效,但问题出现在角色上,当我添加反应时,机器人没有给我任何角色.我真的希望有人可以帮助我使用这个机器人。