简体   繁体   English

我收到错误 Typerror 无法读取未定义的属性“执行”

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

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.我的主要代码我正在尝试制作一个不和谐的机器人我收到了这个错误 Typerror 无法读取未定义的 'execute' 属性我确实有每个解决方案,但它仍然有一些错误,如果有人解决了它,我将不胜感激。 I am trying to make a simple discor bot but the code does not only work please hlep .我正在尝试制作一个简单的 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')
        }
}
}

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

To make this work you need to do two things.要完成这项工作,您需要做两件事。

First you need to separate your on('guildMemberAdd') from your on('message') event.首先,您需要将on('guildMemberAdd')on('message')事件分开。 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.确保您的commands文件夹与index文件位于同一目录中。 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.我的意思是你有client.login()函数的文件。它可能是bot.js或类似的东西。

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.您的client.on('message'事件中的命令处理程序对我client.on('message'所以我假设您对文件夹结构有问题。

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.目前,您需要手动向if链中添加命令。 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'事件现在应该看起来像这样。

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 😉但我认为,一旦你真正掌握了命令,你就可以自己弄清楚😉

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我会出错? 类型错误:“无法读取未定义的属性‘执行’” - Why am I getting error? TypeError: “Cannot read property 'execute' of undefined” 我收到一个错误:无法读取未定义的属性“推送” - I am getting an error: Cannot read property 'push' of undefined 我在控制台中收到此错误:无法读取未定义的属性“ some” - I am getting this error in the console : Cannot read property 'some' of undefined 为什么我越来越无法读取未定义错误的属性? - why I am getting, cannot read property of undefined error? 我收到错误“无法读取未定义的属性“用户名”” - I am getting the error "cannot read property "username" of undefined" 为什么我收到 TypeError:无法读取未定义的属性“执行” - Why am I receiving TypeError: cannot read property 'execute' of undefined 当我可以打印未定义的变量时,为什么会出现“无法读取未定义错误的属性”? - Why Am I Getting “Cannot read property of undefined error” When I Can Print the Undefined Variable? 我收到以下错误:错误TypeError:无法读取未定义的属性'invalid' - I am getting the following error: ERROR TypeError: Cannot read property 'invalid' of undefined 为什么会收到此错误类型错误:无法读取未定义的属性“客户端” - Why am getting this error TypeError: Cannot read property 'client' of undefined 为什么在此示例中出现“无法读取未定义的属性'文本'”? - Why am I getting “Cannot read property 'text' of undefined” in this example?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM