discordjs仅让管理员角色执行特定命令

时间:2019-04-14 17:52:09

标签: javascript discord.js

运行此代码时出现错误:

module.exports.run = async (bot, message, args) => {
    const guildMember = message.member;
    let role = message.guild.roles.find("name", "Verified");
    let role2 = message.guild.roles.find("name", "Unverified");

    if(guildMember.roles.has(role2)) {
        guildMember.setRoles(role)
        message.reply("BY THE POWER OF DISCORD... YOU ARE NOW...\n***VERIFIED*** \nUse husky?verify TO VERIFY!")
        message.channel.send("@everyone TYPE `?verify` TO VERIFY YOUR SELF AND GET ACCESS TO GENERAL AND LOTS MORE!")
    } else {
        message.reply("Sorry, you're already verified!")
    }
}

module.exports.help = {
    name: "verify"
}

有什么想法我做错了吗?

2 个答案:

答案 0 :(得分:0)

我知道了。对于尝试验证命令或查找角色的其他人,请继续。

    if (guildMember.roles.has(role2.id)) {
        guildMember.addRole(role)
        guildMember.removeRole(role2)
        message.reply("BY THE POWER OF DISCORD... YOU ARE NOW...\n***VERIFIED*** \nUse husky?verify TO VERIFY!")
        message.channel.send("TYPE `?verify` TO VERIFY YOUR SELF AND GET ACCESS TO GENERAL AND LOTS MORE!")
    }

    if (guildMember.roles.has(role.id)) {
        message.reply("Sorry, you're already verified!")
    }

答案 1 :(得分:0)

您的第一个错误是,您检查了 guildMember 是否具有角色“ 未验证”,我将其更改为角色“ 已验证< / strong>”。然后您使用了一个名为setRoles()的函数,尽管您未在以前的代码中定义,而且它也不是 discord.js 的函数,所以我认为您想要向 guildMember 添加角色。我将您的setRoles()函数更改为addRole()。然后,我添加了一个新函数,以使用removeRole()函数删除“ 未验证”角色。

尝试使用以下代码:

module.exports.run = async (bot, message, args) => {
    const guildMember = message.member;
    const role = message.guild.roles.find('name', 'Verified');
    const role2 = message.guild.roles.find('name', 'Unverified');

    if (guildMember.roles.has(role.id)) {
        await guildMember.addRole(role);
        await guildMember.removeRole(role2);

        await message.reply('BY THE POWER OF DISCORD... YOU ARE NOW...\n***VERIFIED*** \nUse husky?verify TO VERIFY!');
        message.channel.send('@everyone TYPE `?verify` TO VERIFY YOUR SELF AND GET ACCESS TO GENERAL AND LOTS MORE!');
    } else {
        message.reply("Sorry, you're already verified!");
    }
};