【问题标题】:Discord Bot unexpectedly exits with error when successfully banning someone成功禁止某人时,Discord Bot 意外退出并出现错误
【发布时间】:2021-08-11 10:52:02
【问题描述】:

我遇到的问题是“禁令”案。当我去“+ban”然后提到用户时,它就起作用了。用户被禁止并发送消息,但随后退出并显示有关 Discord API 和权限的消息错误,即使我拥有该机器人的管理员权限。

当我不提及任何人时,它会做它应该做的事情,只是发出“没有人可以禁止”。消息,但随后退出并出现错误 (Error [BAN_RESOLVE_ID]: Couldn't resolve the user ID to ban.)。我需要重新运行代码才能重新启动机器人。

您知道如何让机器人正常运行吗?

const Discord = require('discord.js');
const client = new Discord.Client();

const prefix = "+";

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);

});


client.on('message', msg => {
    const { content } = msg;
    let latency = Date.now() - msg.createdTimestamp;
    let latencyOfAPI = Math.round(client.ws.ping);
    const user = msg.mentions.users.first();
    let banMember = msg.guild.members.ban(user);
    
    if (!content.startsWith(prefix)) return;

    const args = content.slice(prefix.length).trim().split(/ +/g);
    const command = args.shift().toLowerCase();
    
    switch(command) {

        case "ping" : {

            msg.reply("This is the latency between the message and the response: " + latency + "."  + "\nThis is the API latency: " + latencyOfAPI + ".");

            break;
        } 

        case "pong" : {

            msg.reply("ping");
            break
        }

        case "ban" : {
            if (user) {
               banMember;
               msg.reply("The user " + user + " has been banned.") 
            } else {
                 return msg.reply("There is no one to ban.")
            }
            
            break
        }

    }
});
client.login(.....)

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    第一个问题是,即使没有提及成员或没有禁止命令,您仍试图禁止某人。您尝试使用let banMember = msg.guild.members.ban(user) 定义banMember 变量,但在您检查命令是否为“ban”之前,它会调用ban() 方法。你需要在 switch 语句中移动这个ban() 方法。

    其次,您尝试禁止User。如果有人提到,msg.mentions.users.first() 返回UserUsers 没有 ban() 方法,只有 GuildMembers 有。

    您应该使用msg.mentions.members,而不是msg.mentions.users

    您的代码应如下所示:

    client.on('message', (msg) => {
      const { content } = msg;
    
      if (!content.startsWith(prefix)) return;
    
      const args = content.slice(prefix.length).trim().split(/ +/g);
      const command = args.shift().toLowerCase();
    
      switch (command) {
        case 'ping': {
          let latency = Date.now() - msg.createdTimestamp;
          let latencyOfAPI = Math.round(client.ws.ping);
    
          msg.reply(
            `This is the latency between the message and the response: ${latency}.\nThis is the API latency: ${latencyOfAPI}.`,
          );
          break;
        }
    
        case 'pong': {
          msg.reply('ping');
          break;
        }
    
        case 'ban': {
          const member = msg.mentions.members.first();
    
          if (!member) return msg.reply('There is no one to ban.');
    
          msg.guild.members
            .ban(member)
            .then(() => msg.reply(`The member ${member} has been banned.`))
            .catch(console.error);
          break;
        }
      }
    });
    

    【讨论】:

      猜你喜欢
      • 2021-08-10
      • 1970-01-01
      • 2020-11-15
      • 2022-01-20
      • 1970-01-01
      • 2022-11-28
      • 2016-11-29
      • 1970-01-01
      • 2019-07-24
      相关资源
      最近更新 更多