discord.js需要帮助以使响应角色成为机器人

时间:2020-10-10 00:45:44

标签: javascript node.js discord discord.js

我目前正在尝试制作一个反应角色机器人。我已经编写了以下代码,但需要进一步的帮助以使其符合我的要求。

  • 当前需要添加一个反应来添加/删除角色,希望它做出反应来添加角色,不反应来删除角色。
  • 我希望用户始终使用相同的消息来做出反应,而不是每次机器人重新启动时都必须进行新的嵌入消息。 (消息将在特定的频道中显示)

Code:

const { Client, MessageEmbed } = require("discord.js");
const Discord = require("discord.js");
const client = new Client();
const TOKEN = "";

client.login(TOKEN);

client.on("ready", () => {
    console.log("LOGGED IN!");
});

client.on("message", (message) => {
    if (message.author.bot) {
        if (message.embeds) {
            const embedMsg = message.embeds.find((msg) => msg.title === "Server Roles");
            if (embedMsg) {
                message
                    .react("763867011495231538")
                    .then((reaction) => reaction.message.react("763867011469410334"))
                    .then((reaction) => reaction.message.react("763867012950523915"))
                    .then((reaction) => reaction.message.react("763937127733919754")) //continue doing .then for more reactions
                    .then((msg) => console.log("Reacted to messages"))
                    .catch((err) => console.error);
            }
        }
        return;
    }
    if (message.content.toLowerCase() === "?roles")
        if (message.author.id === "350423254940516352") {
            //user id
            {
                const rolesembed = new Discord.MessageEmbed()
                    .setTitle("Server Roles")
                    .setColor("MAGENTA")
                    .setDescription("<:roblox:763867011495231538> - Roblox\n" + "<:minecraft:763867011469410334> - Minecraft\n" + "<:amongus:763867012950523915> - Among Us\n" + "<:r6s:763937127733919754> - Siege");
                message.channel.send(rolesembed);
            }
        }
    client.on("messageReactionAdd", (reaction, user) => {
        if (user.bot) return;

        var roleName = reaction.emoji.name;
        var role = reaction.message.guild.roles.cache.find((role) => role.name.toLowerCase() === roleName.toLowerCase());
        var member = reaction.message.guild.members.cache.find((member) => member.id === user.id);

        if (member.roles.cache.has(role.id)) {
            member.roles
                .remove(role.id)
                .then((member) => {
                    console.log("Removed " + member.user.username + " from the " + role.name + " role.");
                })
                .catch((err) => console.error);
            message.author.send("**" + "You have been removed from the role " + role.name + "**");
        } else {
            member.roles
                .add(role.id)
                .then((member) => {
                    console.log("Added " + member.user.username + " to the " + role.name + " role.");
                })
                .catch((err) => console.error);
            message.author.send("**" + "You have been given the role " + role.name + "**");
        }
    });
});

感谢您的帮助。

0 个答案:

没有答案