Discord.js V12 编写Ban命令报execute非函数错误排查
Discord机器人Ban指令报错排查
报错信息
开发Discord机器人实现Ban(用户封禁)指令时,调用指令终端抛出如下错误:TypeError: client.commands.get(...).execute is not a function at Client.<anonymous> (C:\Users\Lucas\Desktop\Discord BOT\index.js:29:32)
已尝试StackOverflow、discord.js v12官方指南提到的多种解决方案均未生效,以下是项目相关文件的完整代码:
相关代码
index.js
const fs = require('fs'); const Discord = require('Discord.js'); const { prefix, token } = require('./config.json'); const client = new Discord.Client(); client.commands = new Discord.Collection(); const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); for (const file of commandFiles) { const command = require(`./commands/${file}`); client.commands.set(command.name, command, Discord); } client.once('ready', () => { console.log('Ready!'); }); client.on('message', message => { if (!message.content.startsWith(prefix) || message.author.bot) return; const args = message.content.slice(prefix.length).trim().split(/ +/); const command = args.shift().toLowerCase(); if (!client.commands.has(command)) return; try { client.commands.get(command).execute(message, args, Discord); } catch (error) { console.error(error); message.reply('there was an error trying to execute that command!'); } if (command.permissions) { const authorPerms = message.channel.permissionsFor(message.author); if (!authorPerms || !authorPerms.has(command.permissions)) { return message.reply('You can not do this!'); } } }); client.login(token);
ban.js(Ban指令文件)
const Discord = require("Discord.js"); module.exports = { name: 'ban', description: 'Banish someone', permissions: 'ADMINISTRATOR', usage: 'ban @user <reason>', run: async (message, client, args) => { if (!target) { return message.channel.send(`${message.author.username} USAGE: ban @user reason`) } if(target.id === message.author.id) { return message.channel.send(`${message.author.username}, You can't ban yourself. YOU FOOL!`) } if(!args[1]) { return message.channel.send(`${message.author.username}, Please give a reason to ban that member`) } let embed = new Discord.MessageEmbed() .setTitle('Action: BAN') .setDescription(`Banned ${target} (${target.id})`) .setColor("0ff") .setFooter(`Banned by ${message.author.tag}`); message.channel.send(embed) target.ban(args[1]) } }
问题原因与修复方案
- 直接触发当前报错的原因:方法名不匹配。
index.js中调用指令时取的是execute属性,但ban.js导出的执行方法命名为run,引擎找不到对应方法就会抛出「不是函数」的错误,二选一统命名即可。 - 命令注册逻辑错误:
Discord.Collection的set方法仅接收键、值两个参数,代码中多传的第三个Discord参数会被直接忽略,无实际作用。 - 权限校验顺序错误:当前代码是先执行指令逻辑,再校验操作用户权限,会出现无权限用户也能触发封禁逻辑的问题,需要把权限校验移动到指令执行之前。
- 指令文件缺失核心变量定义:
ban.js中直接使用了未声明的target变量,运行时会抛出「变量未定义」错误,需要提前从消息提及的成员中提取被封禁目标。 - 传参顺序错位:
index.js调用执行方法时传参顺序为(message, args, Discord),但ban.js中方法接收参数的顺序为(message, client, args),参数对应错位会导致后续逻辑拿不到正确的参数值。 - Ban方法调用不符合规范:discord.js v12版本中
member.ban()接收配置对象作为参数,直接传字符串无法正确记录封禁原因,且多词原因需要拼接后传入。
修复后代码参考
index.js 修正部分
// 命令注册部分移除多余参数 for (const file of commandFiles) { const command = require(`./commands/${file}`); client.commands.set(command.name, command); } client.on('message', message => { if (!message.content.startsWith(prefix) || message.author.bot) return; const args = message.content.slice(prefix.length).trim().split(/ +/); const commandName = args.shift().toLowerCase(); if (!client.commands.has(commandName)) return; const command = client.commands.get(commandName); // 权限校验前置 if (command.permissions) { const authorPerms = message.channel.permissionsFor(message.author); if (!authorPerms || !authorPerms.has(command.permissions)) { return message.reply('You can not do this!'); } } try { // 统一调用run方法,传参顺序和指令文件定义对齐 command.run(message, client, args); } catch (error) { console.error(error); message.reply('there was an error trying to execute that command!'); } });
ban.js 修正部分
const Discord = require("Discord.js"); module.exports = { name: 'ban', description: 'Banish someone', permissions: 'ADMINISTRATOR', usage: 'ban @user <reason>', run: async (message, client, args) => { // 提前提取被封禁目标 const target = message.mentions.members.first(); if (!target) { return message.channel.send(`${message.author.username} USAGE: ban @user reason`) } if(target.id === message.author.id) { return message.channel.send(`${message.author.username}, You can't ban yourself. YOU FOOL!`) } if(!args[1]) { return message.channel.send(`${message.author.username}, Please give a reason to ban that member`) } let embed = new Discord.MessageEmbed() .setTitle('Action: BAN') .setDescription(`Banned ${target} (${target.id})`) .setColor("0ff") .setFooter(`Banned by ${message.author.tag}`); message.channel.send(embed) // 修正ban方法传参,拼接多词封禁原因 target.ban({ reason: args.slice(1).join(' ') }) } }
内容的提问来源于stack exchange,提问作者user19363517
相关产品推荐
相关产品推荐

