【发布时间】:2021-02-12 21:36:39
【问题描述】:
我的主要代码我正在尝试制作一个不和谐的机器人我收到此错误 Typerror Cannot read property of 'execute' undefined 我几乎拥有所有解决方案,但如果有人解决了它仍然有一些错误,将不胜感激。我正在尝试制作一个简单的 discor 机器人,但代码不仅可以工作,请帮忙。
const client = new Discord.Client();
const prefix = '-';
const fs = require('fs');
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);
}
client.once('ready', () => {
console.log('yes its online');
});
client.on('message', message =>{
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
client.on('guildMemberAdd', member =>{
const channel = member.guild.channels.cache.find(channel=> channel.name === "hlo");
if(!channel) return;
channel.send(`Welcome to our server , ${member}, please read the rules!`);
});
if(command === 'ping'){
message.channel.send('pong!');
}
if (command == 'kick'){
client.commands.get('kick').execute(message, args);
}
if (command === 'ban'){
client.commands.get('ban').execute(message, args);
}
}); ```
My ban code
```module.exports = {
name: 'ban',
description: "Uses ban hammer",
execute(messsage, args){
if (command === "ban"){
const userBan = message.mentions.users.first();
if(userBan){
var member = message.guild.member(userBan);
if(member) {
member.ban({
reason: 'you broke rules buddy.'
}).then(() => {
message.reply(`${userBan.tag} was banned from the server.`)
})
} else{
message.reply('that user is not in the server.');
}
}else {
message.reply('you need to state a user to ban')
}
}
}
我的踢码
module.exports = {
name: 'kick',
description: 'kick people',
execute(messsage, args){
if (command === "kick"){
const userKick = message.mentions.users.first();
if(userBan){
var member = message.guild.member(userKick);
if(member) {
member.kick({
reason: 'you broke rules buddy.'
}).then(() => {
message.reply(`${userKick.tag} was kicked from the server.`)
})
} else{
message.reply('that user is not in the server.');
}
}else {
message.reply('you need to state a user to kick')
}
}
}```
【问题讨论】:
-
您的代码在我看来相当混乱。
guildMemberAdd事件似乎在message事件内?如果你能说明你做了什么来得到这个错误,或者只是发布完整的错误日志,这也会很有帮助。 -
const commandFiles = fs.readdirSync('./commands/').filter...运行后 commandFiles 的值是多少?看起来集合中不存在 client.commands.get('kick')
标签: javascript node.js discord.js