当我尝试创建一个角色时,它给了我一个错误 (discord.js)

时间:2021-02-20 14:48:45

标签: discord.js

所以我试图发出一个使成员静音的命令。我希望我的机器人检查是否有角色名称静音,如果没有,则为静音成员创建一个角色。 这是代码:

module.exports = {
name: 'mute',
description: 'mutes a member',
execute(message, args, Discord, bot) {
    let mutedRole = message.guild.roles.cache.find(r => r.name === 'Muted')

    if(!mutedRole) {
        Guild.roles.create({
            data: {
                name: 'Muted',
                color: 'BLACK'
            }
        })
    }
}

} 错误是这样的:

C:\Users\ADRIAN\Desktop\ZerOne BOT\commands\mute.js:10
        Guild.roles.create({
                    ^

类型错误:无法读取未定义的属性“创建”

1 个答案:

答案 0 :(得分:0)

我认为您不应该通过代码创建角色。我认为您应该在 Discord 服务器中创建一个角色(就像实际上只是在没有代码的情况下创建一个角色)。下载 ms 包。 (npm i ms) 然后,将此代码粘贴到:

const ms = require('ms');
module.exports = {
    name: "mute",
    description: "Mutes a member!",
    execute(message, args, Discord, bot){
        const target = message.mentions.users.first();
        if (target) {
            let mainRole = message.guild.roles.cache.find(role => role.name === 'Member');
            let muteRole = message.guild.roles.cache.find(role => role.name === 'Muted');

            let memberTarget = message.guild.members.cache.get(target.id);

            if (!args[1]){
                memberTarget.roles.remove(mainRole.id);
                memberTarget.roles.add(muteRole.id);
                message.channel.send(`<@${memberTarget.user.id}> has been muted.`);
                return;
            }

            if (isNaN(args[1]) || !args[1] === ' '){
                message.channel.send("Please enter a real number! (If you have a space after the username and you don\'t want to set a time limit, delete the space. YOU HAVE TO.)");
                return;
            }
            
            memberTarget.roles.remove(mainRole.id);
            memberTarget.roles.add(muteRole.id);
            message.channel.send(`<@${memberTarget.user.id}> has been muted for ${ms(ms(args[1]))}`);

            setTimeout(function(){
                memberTarget.roles.remove(muteRole.id);
                memberTarget.roles.add(mainRole.id);
            }, ms(args[1]));
        } else {
            message.channel.send("Can't find that member!");
        }
    }
}

感谢 CodeLyon 提供此代码。希望这对你有用!它甚至包括时间限制。 格式 1: !mute @User 格式 2:!mute @User 1000(1000 可以更改,只需确保以毫秒为单位)

module.exports = {
    name: "unmute",
    description: "Unmutes a member!",
    execute(message, args, Discord, bot){
        const target = message.mentions.users.first();
        if (target) {
            let mainRole = message.guild.roles.cache.find(role => role.name === 'Member');
            let muteRole = message.guild.roles.cache.find(role => role.name === 'Muted');

            let memberTarget = message.guild.members.cache.get(target.id);

            memberTarget.roles.remove(muteRole.id);
            memberTarget.roles.add(mainRole.id);
            message.channel.send(`<@${memberTarget.user.id}> has been unmuted.`);
        } else {
            message.channel.send("Can't find that member!");
        }
    }
}

格式:!unmute @User

请记住,此代码仅在您有角色 Member 和角色 Muted 时才有效。因此,在您的服务器中,将角色 Member 添加到每个人,并添加一个静音角色(不是每个人)。

希望这有效!如果有任何需要澄清的地方,请在评论中告诉我。