【发布时间】:2021-05-22 14:46:55
【问题描述】:
所以我最近开始制作一个不和谐的机器人,我是编码新手,但我确实有一些基本知识! 所以我主要是编写代码,它的工作率为 90%。唯一的问题是在添加或删除卷时。因此,当用户点击表情以获得角色时,机器人会对点击做出反应,但无法定义“公会”,因此它不会赋予角色。
module.exports = {
name: 'reactionrole',
description: "Best thing!!!",
async execute(message, args, Discord, bot) {
const channel = 'the id channel';
const modRole = message.guild.roles.cache.find(role => role.name === "Mod");
const defaultRole = message.guild.roles.cache.find(role => role.name === "El Gars");
const modEmoji = '????';
const defaultEmoji = '????';
//========== My embed message with the info ========//
let embed = new Discord.MessageEmbed()
.setColor('#e42643')
.setTitle(`just thing`)
.setDescription(`just thing\n\n`
+ `${modEmoji} bla bla bla\n`
+ `${defaultEmoji} bla bla bla`);
let messageEmbed = await message.channel.send(embed);
//========== Letting my bot add the reaction ======//
messageEmbed.react(modEmoji);
messageEmbed.react(defaultEmoji);
//========== Here to add the role =================//
bot.on('messageReactionAdd', async (reaction, user) => {
if (reaction.message.partial) await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();
if (user.bot) return;
if (!reaction.message.guild) return;
if (reaction.message.channel.id == channel) {
if (reaction.emoji.name === modEmoji) {
await reaction.message, guild, members.cache.get(user.id).roles.add(modRole);
}
if (reaction.emoji.name === defaultEmoji) {
await reaction.message, guild, members.cache.get(user.id).roles.add(defaultRole);
}
} else {
return;
}
});
//========= Here to remove the role ============//
bot.on('messageReactionRemove', async (reaction, user) => {
if (reaction.message.partial) await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();
if (user.bot) return;
if (!reaction.message.guild) return;
if (reaction.message.channel.id == channel) {
if (reaction.emoji.name === modEmoji) {
await reaction.message, guild, members.cache.get(user.id).roles.remove(modRole);
}
if (reaction.emoji.name === defaultEmoji) {
await reaction.message, guild, members.cache.get(user.id).roles.remove(defaultRole);
}
} else {
return;
}
});
}
}
//========== In my main.js =========//
const Discord = require("discord.js");
const config = require("./config.json");
const fs = require('fs');
const bot = new Discord.Client({ partials: ["MESSAGE", "CHANNEL", "REACTION"] });
bot.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
bot.commands.set(command.name, command)
}
bot.on("ready", () => {
console.log('Bot has started.');
});
bot.on('guildMemberAdd', guildMember => {
let welcomeRole = guildMember.guild.roles.cache.find(role => role.name === 'El Gars');
guildMember.roles.add(welcomeRole);
guildMember.guild.channels.cache.get('id channel').send(`Yo <@${guildMember.user.id}> kes tu criss icit!`)
});
bot.on('message', message => {
if (!message.content.startsWith(config.prefix) || message.author.bot) return;
const args = message.content.slice(config.prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'command') {
bot.commands.get('command').execute(message, args, Discord);
}
if (command === 'clear') {
bot.commands.get('clear').execute(message, args);
} else if (command === 'ban') {
bot.commands.get('ban').execute(message, args);
} else if (command === 'kick') {
bot.commands.get('kick').execute(message, args);
} else if (command === 'mute') {
bot.commands.get('mute').execute(message, args);
} else if (command === 'unmute') {
bot.commands.get('unmute').execute(message, args);
} else if (command === 'reactionrole') {
bot.commands.get('reactionrole').execute(message, args, Discord, bot);
}
});
bot.login(config.token);
【问题讨论】:
标签: javascript discord.js roles