Discord.js静音命令仅发送不正确命令的嵌入 [英] Discord.js Mute command only sends embed for incorrect command

查看:57
本文介绍了Discord.js静音命令仅发送不正确命令的嵌入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我为我的不和谐机器人发出了一个静音命令,该命令会发现是否存在静音"提示.角色存在,如果不存在,则漫游器会创建一个静音"角色.角色,然后将该角色赋予所提到的成员,当前,当我运行该命令时,它只给我嵌入了如果命令编写不正确而应该发送的嵌入内容.

So im making a mute command for my discord bot which finds if a "Muted" role exists and if it doesn't then the bot creates a "Muted" role and then gives that role to the mentioned member and currently when i run the command it only gives me the embed that its supposed to send if the command was written incorrectly.

const BaseCommand = require('../../utils/structures/BaseCommand');
const Discord = require('discord.js');

module.exports = class MuteCommand extends BaseCommand {
  constructor() {
    super('mute', 'moderation', []);
  }

  async run(client, message, args) {
    if(!message.member.hasPermission("MUTE_MEMBERS")) return message.channel.send("You do not have Permission to use this command.");
    if(!message.guild.me.hasPermission("MUTE_MEMBERS")) return message.channel.send("I do not have Permissions to mute members.");
    const Embedhelp = new Discord.MessageEmbed()
    .setTitle('Mute Command')
    .setColor('#6DCE75')
    .setDescription('Use this command to Mute a member so that they cannot chat in text channels nor speak in voice channels')
    .addFields(
      { name: '**Usage:**', value: '=mute (user) (time) (reason)'},
      { name: '**Example:**', value: '=mute @Michael stfu'},
      { name: '**Info**', value: 'You cannot mute yourself.\nYou cannot mute me.\nYou cannot mute members with a role higher than yours\nYou cannot mute members that have already been muted'}
   )
    .setFooter(client.user.tag, client.user.displayAvatarURL());

    let role = 'Muted'
    let muterole = message.guild.roles.cache.find(x => x.name === role);
    if (typeof muterole === undefined) {
      message.guild.roles.create({
        data: {
          name: 'muted',
          color: '#ff0000',
          permissions: {
              SEND_MESSAGES: false,
              ADD_REACTIONS: false,
              SPEAK: false
          }
        },
        reason: 'to mute people',
      })
      .catch(err => console.log(err).then(message.channel.send('Mute Role could not be created')))
    } 

    const mentionedMember = message.mentions.members.first() || await message.guild.members.fetch(args[0]);
    let reason = args.slice(1).join(" ");
    const banEmbed = new Discord.MessageEmbed()
     .setTitle('You have been Muted in '+message.guild.name)
     .setDescription('Reason for Mute: '+reason)
     .setColor('#6DCE75')
     .setTimestamp()
     .setFooter(client.user.tag, client.user.displayAvatarURL());

   if (!reason) reason = 'No reason provided';
   if (!args[0]) return message.channel.send(Embedhelp);
   if (!mentionedMember) return message.channel.send(Embedhelp);
   if (!mentionedMember.bannable) return message.channel.send(Embedhelp);
   if (mentionedMember.user.id == message.author.id) return message.channel.send(Embedhelp);
   if (muterole = undefined) return message.channel.send(Embedhelp);
   if (mentionedMember.user.id == client.user.id) return message.channel.send(Embedhelp);
   if (mentionedMember.roles.cache.has(muterole)) return message.channel.send(Embedhelp);
   if (message.member.roles.highest.position <= mentionedMember.roles.highest.position) return message.channel.send(Embedhelp);

   await mentionedMember.send(banEmbed).catch(err => console.log(err));
   await mentionedMember.roles.add(muterole).catch(err => console.log(err).then(message.channel.send('There was an error while muting the member')))

  } 
}

我仍然无法找出问题所在以及为什么这样做,我非常想知道代码中的错误以及是否还有我不知道的错误.

I am still unable to find out what the problem is and why it does this, i would very much like to know the error in my code and if there are any more erros that i am unaware of.

推荐答案

我们可以看到您的代码:

We can see your code:


    let role = 'Muted'
    let muterole = message.guild.roles.cache.find(x => x.name === role);
    if (typeof muterole === undefined) {
      message.guild.roles.create({
        data: {
          name: 'muted',
          color: '#ff0000',
          permissions: {
              SEND_MESSAGES: false,
              ADD_REACTIONS: false,
              SPEAK: false
          }
        },
        reason: 'to mute people',
      })
      .catch(err => console.log(err).then(message.channel.send('Mute Role could not be created')))
    } 

if (muterole = undefined) return message.channel.send(Embedhelp);

,这将在未定义互斥锁时停止运行.由于无法创建互斥锁,因此在运行if互斥锁行时它将停止运行.要解决此问题,在discord.js中创建角色时,仅当您想向角色添加权限时才需要权限标志.您无需在角色中指定不需要/否的权限为false/deny,因为如果您不将其标记出来,它会将所有权限标记为false.因此,我们可以仅使用方括号替换权限:

and this will stop the runnning while muterole is undefined. Since the muterole is not able to create, it will stop running while running to the if muterole line. To fix the problem, while creating a role in discord.js, permissions flag is needed only when you want to add the permissions to the role. You don't have to put false/deny to specific which permissions you don't want in the role since it's marking all the permissions as false if you don't't label them out. Therefore, we could replace the permissions with only bracket:

if (muterole === undefined) {
    message.guild.roles.create({
        data: {
            name: 'muted',
            color: '#ff0000',
            permissions: []
        },
        reason: 'to mute people',
    })
        .catch(err => console.log(err).then(message.channel.send('Mute Role could not be created')))
}

这篇关于Discord.js静音命令仅发送不正确命令的嵌入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆