问题
My main code I am trying to make a discord bot i was getting this error Typerror Cannot read property of 'execute' undefined i have literally every solution but it still has some errors would be grateful if someone solved it. I am trying to make a simple discor bot but the code does not only work please hlep .
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')
}
}
}
my kick code
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')
}
}
}```
回答1:
To make this work you need to do two things.
First you need to separate your on('guildMemberAdd')
from your on('message')
event. Right now they are bungled together and that won't work.
So your index file should be
// your command reader above this
client.once('ready', () => {
console.log('yes its online');
});
client.on('guildMemberAdd', member => {
// your code
}
client.on('message', message => {
// here we will work in part two
}
Secondly, lets take a look at your command reader. That portion of your code looks good to me. Make sure that your commands
folder is in the same directory as your index
file. This is where I assume your problem lies.
Note: I say "index file" here. What I mean by that is the file you have the client.login()
function in. It might be bot.js
or something similar for you.
Your folder structure should looks something like this
-- Your bot folder
- index.js
- package.json
- package-lock.json
-- commands (this is a folder)
- kick.js
- ban.js
Note: Just to make sure, here is a command reader that definetly works with the above file structure.
// Read all files in the commands folder and that ends in .js
const commands = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
// Loop over the commands, and add all of them to a collection
// If there's no name found, prevent it from returning an error
for (let file of commands) {
const command = require(`./commands/${file}`);
// Check if the command has both a name and a description
if (command.name && command.description) {
client.commands.set(command.name, command);
} else {
console.log("A file is missing something");
continue;
}
// check if there is an alias and if that alias is an array
if (command.aliases && Array.isArray(command.aliases))
command.aliases.forEach(alias => client.aliases.set(alias, command.name));
};
Your command handler inside your client.on('message'
event works for me. So I assume you have your problem with the folder structure.
That being said, I would like to suggest you use a slightly different way of handling your commands. Currently you need to manually add a command to your if
chain. Thats not really efficient.
Ideally you want to do that automatically. You already have your arguments and your command word sparated. All you need to do now is check if that command exists and if it does, execute it.
// check if there is a message after the prefix
if (command.length === 0) return;
// look for the specified command in the collection of commands
let cmd = client.commands.get(command);
// if there is no command we return with an error message
if (!cmd) return message.reply(`\`${prefix + command}\` doesn't exist!`);
// finally run the command
cmd.execute(message, args);
Your client.on('message'
event should now look a little something like this.
client.on('message', message => {
// check if the author is a bot
if (message.author.bot) return;
// check if the message comes through a DM
if (message.guild === null) return;
// check if the message starts with the prefix
if (!message.content.startsWith(prefix)) return;
// slice off the prefix and convert the rest of the message into an array
const args = message.content.slice(prefix.length).trim().split(/ +/g);
// convert all arguments to lowercase
const command = args.shift().toLowerCase();
// check if there is a message after the prefix
if (command.length === 0) return;
// look for the specified command in the collection of commands
let cmd = client.commands.get(command);
// if there is no command we return with an error message
if (!cmd) return message.reply(`\`${prefix + command}\` doesn't exist!`);
// finally run the command
cmd.execute(message, args);
});
Note: I also noticed that you have some inconsistencies in your commands. But I assume that once you actually get to the commands you will be able to figure that out yourself 😉
来源:https://stackoverflow.com/questions/64608116/i-am-getting-an-error-typerror-cannot-read-property-execute-of-undefined