【问题标题】:How do I create a kick command for a discord bot?如何为不和谐机器人创建踢命令?
【发布时间】:2020-04-29 05:39:18
【问题描述】:

看过这篇文章的人你好。我需要帮助来为我的不和谐机器人创建踢命令。我正在使用 discord.jsnode.js 。我确实有像 const Discord = require('discord.js'); const Client = new Discord.Client(); 这样的开始。我正在使用 Visual Studio Code 对其进行编码。我真的不明白还能做什么。我曾尝试在 YouTube 上寻求帮助,但每次尝试似乎都不再有效。有人可以指导我吗?谢谢:D

【问题讨论】:

标签: node.js json discord discord.js


【解决方案1】:

大公会https://discordjs.guide,您可以在这里找到所有信息。

创建 kick 命令的一种方法是使用此代码。但是对于新手用户来说,kick 命令相当复杂。它需要考虑到许多细微差别,从一开始就尝试实现一些更简单的东西。

const Discord = require('discord.js');
const bot = new Discord.Client();
bot.on('message', message => {
    if(message.content.startWith('!kick') {
        if(message.channel.type === 'DM') {
            //Fist check if message channel is not direct message, because you cant kick out of guide 
            message.channel.send('This command can use only in guide');
            return;
        };

        //Then check if user have permissions to do that
        if(!message.member.hasPermission('KICK_MEMBERS')) {
            message.channel.send('You have no permissions to do that');
            return;
        };

        //const a member, wich you need yo kick (its fist mention message member)
        let mentionMember = message.mentions.members.first();
        //If user dont mention a member, that show him this error msg
        if(!mentionMember) {
            message.channel.send('pls mention member witch you need to kick');
            return;
        }

        //Get the highest role of user for compare
        let authorHighestRole = message.member.highestRole.position;
        let mentionHighestRole = mentionMember.highestRole.position;

        //If mention user have same or higher role, so show this error msg
        if(mentionHighestRole >= authorHighestRole) {
            message.channel.send('You can`t kick members with equal or higher position');
            return;
        };

        //Check if your bot can`t kick this user, so that show this error msg 
        if(!mentionMember.kickable) {
            message.channel.send('I have no permissions to kick this user');
            return
        };

        //If all steps are completed successfully try kick this user
        mentionMember.kick()
            .then(() => console.log(`Kicked ${member.displayName}`))
            .catch(console.error);
    };
})

【讨论】:

    【解决方案2】:

    这里...

        client.on('message', message => {
      // Ignore messages that aren't from a guild
      if (!message.guild) return;
    
      // If the message content starts with "!kick"
      if (message.content.startsWith('!kick')) {
        // Assuming we mention someone in the message, this will return the user
        // Read more about mentions over at https://discord.js.org/#/docs/main/master/class/MessageMentions
        const user = message.mentions.users.first();
        // If we have a user mentioned
        if (user) {
          // Now we get the member from the user
          const member = message.guild.member(user);
          // If the member is in the guild
          if (member) {
            /**
             * Kick the member
             * Make sure you run this on a member, not a user!
             * There are big differences between a user and a member
             */
            member
              .kick('Optional reason that will display in the audit logs')
              .then(() => {
                // We let the message author know we were able to kick the person
                message.reply(`Successfully kicked ${user.tag}`);
              })
              .catch(err => {
                // An error happened
                // This is generally due to the bot not being able to kick the member,
                // either due to missing permissions or role hierarchy
                message.reply('I was unable to kick the member');
                // Log the error
                console.error(err);
              });
          } else {
            // The mentioned user isn't in this guild
            message.reply("That user isn't in this guild!");
          }
          // Otherwise, if no user was mentioned
        } else {
          message.reply("You didn't mention the user to kick!");
        }
      }
    });
    

    (来自discord.js github)

    【讨论】:

      【解决方案3】:

      idk if (content == 'kick') { 让用户 = msg.mentions.members.first() if(!user) return msg.reply('请提及一个用户,那不是必须从公会中删除的!')//为什么?我用语法写! 这是不和谐的,没有任何意义! 如果(!user.kickable){ db.set(Kicked-${msg.guild.id}, {userId: user.id, userTag: user.user.tag, guildId: msg.guild.id, guildName: msg.guild.name, modId: msg.author.id, modName: msg .author.name,isKicked: 踢}) return msg.reply('用户不可踢!')

      踢=假 } db.set(Kicked-${msg.guild.id}, {userId: user.id, userTag: user.user.tag, guildId: msg.guild.id, guildName: msg.guild.name, modId: msg.author.id, modName: msg .author.name,isKicked: 踢}) 踢=真

          user.kick('Kicked was by ' + msg.author.tag)
      

      }

      如果(内容 == '测试'){ 让 modname = db.get(Kicked-${msg.guild.id}).modName msg.reply(modname) }

      【讨论】:

        猜你喜欢
        • 2020-09-23
        • 2021-01-24
        • 1970-01-01
        • 2021-11-21
        • 2021-06-30
        • 2021-08-13
        • 2018-07-21
        • 2021-09-19
        • 2018-11-10
        相关资源
        最近更新 更多