所以我现在使用的代码给了我一个 TypeError: Cannot read property 'send' of undefined for my ping 命令,然后 TypeError: Cannot read property 'users' of undefined for my ban 命令,我还有其他命令由于我做了或忘记做的事情而无法正常工作,代码一直在工作,直到我向代码添加权限然后一切都在走下坡路,我无法弄清楚哪里不起作用/它是如何工作的在权限之前,甚至在一段时间之后,然后停止工作,我不知道我是忘记添加一行还是忘记了;但我很确定它在这方面很好,但任何帮助表示赞赏。
这是我的 main.js
const client = new Discord.Client({ partials: ["MESSAGE", "CHANNEL", "REACTION"]});
//const client = require('discord-buttons');
const fs = require('fs');
require('dotenv').config();
//const prefix = '-';
client.commands = new Discord.Collection();
client.events = new Discord.Collection();
['command_handler', 'event_handler'].forEach(handler =>{
require(`./handlers/${handler}`)(client, Discord);
})
client.login(process.env.DISCORD_TOKEN);
这是我的命令处理程序
const { Client } = require('discord.js');
const fs = require('fs');
module.exports = (Client, Discord) =>{
const command_files = fs.readdirSync(`./commands/`).filter(file => file.endsWith('.js'));
command.execute(client, message, args, cmd, Discord);
for(const file of command_files){
const command = require(`../commands/${file}`);
if(command.name){
Client.commands.set(command.name, command);
} else {
continue;
}
}
}
这是我的事件 handler.js
const { Client } = require('discord.js');
const fs = require('fs');
module.exports = (Client, Discord) =>{
const load_dir = (dirs) =>{
const event_files = fs.readdirSync(`./events/${dirs}`).filter(file => file.endsWith('.js'));
for(const file of event_files){
const event = require(`../events/${dirs}/${file}`);
const event_name = file.split('.')[0];
Client.on(event_name, event.bind(null, Discord, Client));
}
}
['client', 'guild'].forEach(e => load_dir(e));
}
这是我的 message.js
const cooldowns = new Map();
module.exports = (Discord, client, message) =>{
const prefix = 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));
if(!command) return;
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 = {
name: 'ping',
cooldown: 10,
permissions: ["SEND_MESSAGES"],
description: "this is a ping command!",
execute(client, message, args, cmd, Discord){
message.channel.send('pong!');
}
}
这是我的ban.js
module.exports = {
name: 'ban',
aliases: ['b'],
permissions: ["ADMINISTRATOR", "BAN_MEMBERS",],
description: "this is a ban command!",
execute(client, message, args, cmd, Discord){
const member = message.mentions.users.first();
if(member){
const memberTarget = message.guild.members.cache.get(member.id);
memberTarget.ban();
message.channel.send('User has been banned');
}else{
message.channel.send('Need to mention a member you wish to ban')
}
}
}
答案 0 :(得分:0)
这是一个非常明显的错误,你给出了错误的定义,你做了一个
command.execute(message,args, cmd, client, Discord);
在你的命令文件中,你已经使用了
execute(client, message, args, cmd, Discord){
您可以通过在命令处理程序中使用以下内容来解决此问题:
command.execute(client, message, args, cmd, Discord);
希望对您有所帮助,如果您需要更多帮助,请在下方评论。