Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

javascript - Cannot read property 'execute' of undefined - discord

I am coding a discord bot. The commands I'm having issues with is temprole and kick. The ping command seems to work fine.

main.js

const Discord = require('discord.js');

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('Bot is 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();

    if (command == 'ping') {
        client.commands.get('ping').execute(message, args);
    } else if (command == 'temprole') {
        client.commands.get('temprole').execute(message, args);
    }
});

client.login('token')

temprole.js

module.exports = {
     name: 'youtube',
     description: "give the member a temprole",
     execute(message, args) {

         if (message.member.roles.cache.has('798180573998612512')) {

         } else {
             message.channel.send('You dont have access to this command!')
         }
     }
 }

kick.js

module.exports = {
    name: 'kick',
    description: "This command kicks a member!",
    execute(messages, args) {
        const member = message.mentions.users.first();
        if (member) {
            const memberTarger = message.guild.members.cache.get(member.id);
            memberTarger.kick();
            message.channel, send('User has been kicked');
        } else {
            message.channel.send('You coundt kick that member');
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In your temprole.js, the "name" property does not match temprole, therefore the commands collection will set the command name as "youtube". You then try to get the "temprole" command from the collection but there aren't any. The fix is to change your command name's property to "temprole".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...