如何为不和谐服务器制作“!踢”命令消息?

Posted

技术标签:

【中文标题】如何为不和谐服务器制作“!踢”命令消息?【英文标题】:How do i make a "!kick" command message for discord server? 【发布时间】:2020-09-09 16:12:46 【问题描述】:

我正在尝试制作自己的不和谐版主机器人!我需要''!kick''命令的帮助,比如如果我想踢某人,我只需使用 !kick @user

从诸如

之类的东西开始
const discord = require('discord.js');
const client = new discord.Client;

然后

client.on('ready', () => 
    console.log('This bot is running.');
);

client.on('message', msg => 

然后我的踢命令应该会执行!

【问题讨论】:

【参考方案1】:

我在discordjs documentation找到了一些代码

// Import the discord.js module
const Discord = require('discord.js');

// Create an instance of a Discord client
const client = new Discord.Client();

/**
 * The ready event is vital, it means that only _after_ this will your bot start reacting to information
 * received from Discord
 */
client.on('ready', () => 
  console.log('I am ready!');
);

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

// Log our bot in using the token from https://discordapp.com/developers/applications/me
client.login('your token here');

这个的主要代码是member.kick('Optional reason that will display in the audit logs'),但你需要将它嵌套在某些条件语句中,以检查成员是否存在以及编写命令的人是否是管理员。

【讨论】:

Np,编码愉快。 我有 youtube 和 twitch ,哈哈 thx 无论如何,我注意到我还有 1 个子

以上是关于如何为不和谐服务器制作“!踢”命令消息?的主要内容,如果未能解决你的问题,请参考以下文章

如何让不和谐的机器人踢出 Java 中的成员?

如何为不和谐的机器人制作一个可变的嵌入列表?

discord python bot kick 命令

加入不和谐服务器时如何为特定成员添加角色

如何为不和谐机器人调用异步函数

试图制作一个命令页面来列出我的不和谐机器人上的所有命令,但不知道如何[关闭]