如何为Discord机器人创建kick命令?

时间:2020-01-12 04:08:32

标签: node.js json discord discord.js

您好,查看过此内容的人。我需要为不和谐的bot创建kick命令的帮助。我正在使用ESCdiscord.js。我确实有像node.js const Discord = require('discord.js');这样的开始。我正在使用Visual Studio Code对其进行编码。我真的不明白该怎么办。我曾尝试在YouTube上寻求帮助,但每次尝试都似乎不再起作用。有人可以指导我吗?谢谢:D

3 个答案:

答案 0 :(得分:0)

伟大公会的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);
    };
})

答案 1 :(得分:0)

这里...

    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)

答案 2 :(得分:0)

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) }