静音和取消静音命令 - Discord.js

时间:2021-06-03 12:12:15

标签: discord discord.js bots

我是 JavaScript 新手,我开始为我的 Discord 服务器编写机器人。我确实踢和禁止命令,但我不能做静音和取消静音的。有人可以将他的代码发给我,或者只是检查一下我做了什么(我无法执行取消静音命令,但我几乎没有执行静音命令)?

我的静音命令代码(不起作用):

if (message.content.startsWith(config.prefix + "mute")) {
    if (!message.guild.member(message.author).hasPermission("MENAGE_MESSAGES")) {
        return message.channel.send('You do not have the permission for mute users!');
    }
    if (!message.guild.member(client.user).hasPermission("MENAGE_MESSAGES")) {
        return message.channel.send("I don’t have the permission for mute users!");
    }
    if (message.mentions.users.size === 0) {
        return message.channel.send("You need to ping a user or the user can't be found!");
    }
    let mutedRole = message.guild.roles.find(mR => mR.name === "Muted");
    if(!mutedRole) {
        try {
            mutedRole = await message.guild.createRole({
                name: "Muted",
                color: "#000000",
                permissions: []
            });

            message.guild.channels.forEach(async (channel, id) => {
                await channel.overwritePermissions(mutedRole, {
                    SEND_MESSAGES: false,
                    ADD_REACTIONS: false
                })
            });
    
  }

  if(toMute.roles.has(mutedRole.id)) return message.channel.send("This user is already muted!");
  await toMute.addRole(mutedRole);
}

感谢任何形式的帮助!

1 个答案:

答案 0 :(得分:0)

首先,您没有定义 toMute。您还拼错了权限 MANAGE_MESSAGES。然后对于变量 mutedRole,您忘记了 cache。并且您在 try 之后没有发现任何错误。对于最后两行,您必须使用 GuildMember 对象。

我已更正您的代码,但我强烈建议您再次阅读docs

这是固定代码:

if (message.content.startsWith(config.prefix + "mute")) {
      let toMute = message.mentions.users.first();

      if (!message.guild.member(message.author).hasPermission("MANAGE_MESSAGES")) {
        return message.channel.send('You do not have the permission for mute users!');
      }
      if (!message.guild.member(client.user).hasPermission("MANAGE_MESSAGES")) {
        return message.channel.send("I don’t have the permission for mute users!");
      }
      if (!toMute) {
        return message.channel.send("You need to ping a user or the user can't be found!");
      }
      let mutedRole = message.guild.roles.cache.find(mR => mR.name === "Muted");
      if (!mutedRole) {
        try {
          mutedRole = await message.guild.createRole({
            name: "Muted",
            color: "#000000",
            permissions: []
          });

          message.guild.channels.forEach(async (channel, id) => {
            await channel.overwritePermissions(mutedRole, {
              SEND_MESSAGES: false,
              ADD_REACTIONS: false
            })
          });

        } catch (err) {
          console.log(err.stack);
        }
      }
      if (message.guild.member(toMute).roles.cache.has(mutedRole.id)) return message.channel.send("This user is already muted!");
      await message.guild.member(toMute).roles.add(mutedRole);
    }