簡體   English   中英

我收到錯誤 Typerror 無法讀取未定義的屬性“執行”

[英]I am getting an error Typerror Cannot read property 'execute' of undefined

我的主要代碼我正在嘗試制作一個不和諧的機器人我收到了這個錯誤 Typerror 無法讀取未定義的 'execute' 屬性我確實有每個解決方案,但它仍然有一些錯誤,如果有人解決了它,我將不勝感激。 我正在嘗試制作一個簡單的 discor bot,但代碼不僅有效,請 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')
        }
}
}

我的踢碼

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')
        }
}
}```

要完成這項工作,您需要做兩件事。

首先,您需要將on('guildMemberAdd')on('message')事件分開。 現在他們被搞砸了,這是行不通的。

所以你的索引文件應該是

// 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
}

其次,讓我們看看你的命令閱讀器。 你的那部分代碼對我來說看起來不錯。 確保您的commands文件夾與index文件位於同一目錄中。 這就是我假設您的問題所在。

注意:我在這里說“索引文件”。 我的意思是你有client.login()函數的文件。它可能是bot.js或類似的東西。

你的文件夾結構應該是這樣的

-- Your bot folder
    - index.js
    - package.json
    - package-lock.json
    -- commands (this is a folder)
        - kick.js
        - ban.js

注意:為了確保,這里有一個命令閱讀器,它可以與上述文件結構一起使用。

// 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));
};

您的client.on('message'事件中的命令處理程序對我client.on('message'所以我假設您對文件夾結構有問題。

話雖如此,我還是建議您使用稍微不同的方式來處理您的命令。 目前,您需要手動向if鏈中添加命令。 那不是很有效。

理想情況下,您希望自動執行此操作。 您已經將參數和命令字分開了。 您現在需要做的就是檢查該命令是否存在,如果存在,則執行它。

// 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);

您的client.on('message'事件現在應該看起來像這樣。

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);
});

注意:我還注意到您的命令中存在一些不一致之處。 但我認為,一旦你真正掌握了命令,你就可以自己弄清楚😉

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM