【问题标题】:How do I code a discord bot not stop after a incorrect command如何编写不正确的命令后不停止的不和谐机器人
【发布时间】:2021-09-18 07:05:22
【问题描述】:

所以每当我发送一个不正确的命令,比如 !pong(其中!ping 是正确的命令),机器人就会返回以下错误:

(if(command.permissions.length){ ^

TypeError:无法读取未定义的属性“权限”

即使我删除了整个权限代码,它也会给我一个

TypeError: 无法读取未定义的属性“用户”

只有当我有一个错误的命令时 !pong 没有命令或 !me 任何前缀不是实际命令的东西。下面是我使用的代码

消息.js

require('dotenv').config();

const 冷却时间 = new Map();

module.exports = (Discord, 客户端, 消息) =>{ 常量前缀 = process.env.PREFIX; if(!message.content.startsWith(prefix) || message.author.bot) return;

const args = message.content.slice(prefix.length).split(/ +/);
const cmd = args.shift().toLowerCase();

const command = client.commands.get(cmd) || client.commands.find(a => a.aliases && a.aliases.includes(cmd));
const validPermissions = [
    "CREATE_INSTANT_INVITE",
    "KICK_MEMBERS",
    "BAN_MEMBERS",
    "ADMINISTRATOR",
    "MANAGE_CHANNELS",
    "MANAGE_GUILD",
    "ADD_REACTIONS",
    "VIEW_AUDIT_LOG",
    "PRIORITY_SPEAKER",
    "STREAM",
    "VIEW_CHANNEL",
    "SEND_MESSAGES",
    "SEND_TTS_MESSAGES",
    "MANAGE_MESSAGES",
    "EMBED_LINKS",
    "ATTACH_FILES",
    "READ_MESSAGE_HISTORY",
    "MENTION_EVERYONE",
    "USE_EXTERNAL_EMOJIS",
    "VIEW_GUILD_INSIGHTS",
    "CONNECT",
    "SPEAK",
    "MUTE_MEMBERS",
    "DEAFEN_MEMBERS",
    "MOVE_MEMBERS",
    "USE_VAD",
    "CHANGE_NICKNAME",
    "MANAGE_NICKNAMES",
    "MANAGE_ROLES",
    "MANAGE_WEBHOOKS",
    "MANAGE_EMOJIS",
  ]

  if(command.permissions.length){
    let invalidPerms = []
    for(const perm of command.permissions){
      if(!validPermissions.includes(perm)){
        return console.log(`Invalid Permissions ${perm}`);
      }
      if(!message.member.hasPermission(perm)){
        invalidPerms.push(perm);
      }
    }
    if (invalidPerms.length){
      return message.channel.send(`Missing Permissions: \`${invalidPerms}\``);
    }
  }



if(!cooldowns.has(command.name)){
    cooldowns.set(command.name, new Discord.Collection());
}

const current_time = Date.now();
const time_stamps = cooldowns.get(command.name);
const cooldown_ammount = (command.cooldown) * 1000;

if(time_stamps.has(message.author.id)){
    const expiration_time = time_stamps.get(message.author.id) + cooldown_ammount;

    if(current_time < expiration_time){
        const time_left = (expiration_time - current_time) / 1000;

        return message.reply(`Please wait ${time_left.toFixed(1)} more seconds before using ${command.name}`)
    }
}

time_stamps.set(message.author.id, current_time);
setTimeout(() => time_stamps.delete(message.author.id), cooldown_ammount);

try{
    command.execute(message,args, cmd, client, Discord);
} catch (err){
    message.reply("There was an error trying to execute this command!");
    console.log(err);
}
 }

ping.js

module.exports = { 名称:'平', 冷却时间:10, 权限:[“SEND_MESSAGES”,], description: "这是一个 ping 命令!", 执行(客户端,消息,参数,cmd,不和谐){ message.channel.send('pong!'); } }

【问题讨论】:

标签: node.js discord.js


【解决方案1】:

在你的message.js中,command的声明后,你需要检查它是undefined还是null

const command = client.commands.get(cmd) || client.commands.find(a => a.aliases && a.aliases.includes(cmd));
if(!command) return;

编辑:您的错误来自这里:

command.execute(message,args, cmd, client, Discord);
//^^how you execute
execute(client, message, args, cmd, Discord){ message.channel.send('pong!'); }
//^^how you define it

如您所见,您将它们按错误的顺序排列。

【讨论】:

  • 谢谢你的工作,当我添加权限代码时我必须删除它,因为它当时工作,但现在我有一个不同的问题(当我回到家时我会查看我是否忘记了添加或不添加其他内容)现在新错误是“TypeError:无法读取未定义的属性'发送'”我可能忘记添加或删除所需的内容
  • 如果有效,请将其标记为已接受。
  • 我也找到了解决您的新错误的方法,请检查我对答案所做的编辑
  • 第二个命令没有修复它现在期望在 command.execute 和 ; 的代码中出现 "," 的错误。最后
猜你喜欢
  • 1970-01-01
  • 2018-11-10
  • 2020-11-18
  • 2018-07-21
  • 2021-06-20
  • 2021-10-21
  • 2022-01-15
  • 2021-06-17
  • 2021-11-24
相关资源
最近更新 更多