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

vdzxcuhz  于 2023-03-07  发布在  Node.js
关注(0)|答案(3)|浏览(187)

大家好,看过这个的人。我需要帮助为我的discord机器人创建一个踢命令。我使用discord.jsnode.js。我有像const Discord = require('discord.js');const Client = new Discord.Client();这样的开始的东西。我用Visual Studio代码编码它。我真的不知道还能做什么。我试着在YouTube上寻找帮助,但是每次我尝试的时候似乎都不起作用了。有人能指导我吗?谢谢:D

aij0ehis

aij0ehis1#

大公会是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);
    };
})
yhxst69z

yhxst69z2#

这里...

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!");
    }
  }
});

(from不一致意见(js github)

insrf1ej

insrf1ej3#

idk if(content == 'kick'){ let user = msg. removes.members.first()if(!user)return msg.reply('请提一个用户,这个用户不需要从公会中移除!')//为什么?我写的是语法!* 这是不一致的,没有任何意义!* if(!user.kickable){ db.set(Kicked-${msg.guild.id},{userId:user.id,用户标签:用户。用户。标签,guildId:msg.guild.id,协会名称:msg.guild.name,modId:msg.author.id,modName:msg.author.name,已踢:已踢})返回消息回复(“用户不可踢!”)
kick = false }数据库设置(Kicked-${msg.guild.id},{用户ID:user.id,用户标签:用户。用户。标签,guildId:msg.guild.id,协会名称:msg.guild.name,模块ID:msg.author.id,模块名称:msg.author.name,已踢:已踢})已踢=真

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

}
如果(内容== '测试'){让模块名称=数据库.获取(Kicked-${msg.guild.id}).模块名称消息.回复(模块名称)}

相关问题