TypeError: Cannot read property 'send' of undefined for my commands and a TypeError: Cannot read property 'users' of undefined for other commands [英] TypeError: Cannot read property 'send' of undefined for my commands and a TypeError: Cannot read property 'users' of undefined for other commands

查看:29
本文介绍了TypeError: Cannot read property 'send' of undefined for my commands and a TypeError: Cannot read property 'users' of undefined for other commands的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我现在使用的代码给了我一个 TypeError: Cannot read property 'send' of undefined for my ping 命令,然后 TypeError: Cannot read property 'users' of undefined for my ban 命令,我还有其他命令由于我做了或忘记做的事情,它不能很好地工作,代码一直在工作,直到我向代码添加权限然后一切都走下坡路,我无法弄清楚哪里不工作/它是如何工作的在权限之前,甚至在一段时间之后,然后停止工作,我不知道我是否忘记了要添加的行或忘记了 ;但我很确定在这方面它没问题,但感谢任何帮助.

So the code I am working with now gives me a TypeError: Cannot read property 'send' of undefined for my ping command, and then TypeError: Cannot read property 'users' of undefined for my ban command, I have other commands that don't work as well due to what I did or forgot to do, the code was all working until I added permissions to the code then it all went downhill, and I can't figure out where is not working/how it worked before the permissions and even after for a little but then just stop working, I don't know if I'm forgetting a line to add or forgot a ; but I'm pretty sure its fine in that regard but any help is appreciated.

这是我的 main.js

this is my main.js

const client = new Discord.Client({ partials: ["MESSAGE", "CHANNEL", "REACTION"]});
//const client = require('discord-buttons');
const fs = require('fs');
require('dotenv').config();
//const prefix = '-';

client.commands = new Discord.Collection();
client.events = new Discord.Collection();

['command_handler', 'event_handler'].forEach(handler =>{
   require(`./handlers/${handler}`)(client, Discord);
})

client.login(process.env.DISCORD_TOKEN);

这是我的命令处理程序

const { Client } = require('discord.js');
const fs = require('fs');

module.exports = (Client, Discord) =>{
    const command_files = fs.readdirSync(`./commands/`).filter(file => file.endsWith('.js'));

    command.execute(client, message, args, cmd, Discord);
    
    for(const file of command_files){
        const command = require(`../commands/${file}`);
        if(command.name){
            Client.commands.set(command.name, command);
        } else {
            continue;
        }
    }
}

这是我的事件 handler.js

This is my event handler.js

const { Client } = require('discord.js');
const fs = require('fs');
module.exports = (Client, Discord) =>{
    const load_dir = (dirs) =>{
        const event_files = fs.readdirSync(`./events/${dirs}`).filter(file => file.endsWith('.js'));

        for(const file of event_files){
            const event = require(`../events/${dirs}/${file}`);
            const event_name = file.split('.')[0];
            Client.on(event_name, event.bind(null, Discord, Client));
        }
    }

    ['client', 'guild'].forEach(e => load_dir(e));
}

这是我的 message.js

This is my message.js


const cooldowns = new Map();


module.exports = (Discord, client, message) =>{
    const prefix = process.env.PREFIX;
    if(!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const cmd = args.shift().toLowerCase();

    const command = client.commands.get(cmd) || client.commands.find(a => a.aliases && a.aliases.includes(cmd));
    if(!command) return;
    const validPermissions = [
        "CREATE_INSTANT_INVITE",
        "KICK_MEMBERS",
        "BAN_MEMBERS",
        "ADMINISTRATOR",
        "MANAGE_CHANNELS",
        "MANAGE_GUILD",
        "ADD_REACTIONS",
        "VIEW_AUDIT_LOG",
        "PRIORITY_SPEAKER",
        "STREAM",
        "VIEW_CHANNEL",
        "SEND_MESSAGES",
        "SEND_TTS_MESSAGES",
        "MANAGE_MESSAGES",
        "EMBED_LINKS",
        "ATTACH_FILES",
        "READ_MESSAGE_HISTORY",
        "MENTION_EVERYONE",
        "USE_EXTERNAL_EMOJIS",
        "VIEW_GUILD_INSIGHTS",
        "CONNECT",
        "SPEAK",
        "MUTE_MEMBERS",
        "DEAFEN_MEMBERS",
        "MOVE_MEMBERS",
        "USE_VAD",
        "CHANGE_NICKNAME",
        "MANAGE_NICKNAMES",
        "MANAGE_ROLES",
        "MANAGE_WEBHOOKS",
        "MANAGE_EMOJIS",
      ]
    
      if(command.permissions.length){
        let invalidPerms = []
        for(const perm of command.permissions){
          if(!validPermissions.includes(perm)){
            return console.log(`Invalid Permissions ${perm}`);
          }
          if(!message.member.hasPermission(perm)){
            invalidPerms.push(perm);
          }
        }
        if (invalidPerms.length){
          return message.channel.send(`Missing Permissions: `${invalidPerms}``);
        }
      }
    
    

    if(!cooldowns.has(command.name)){
        cooldowns.set(command.name, new Discord.Collection());
    }

    const current_time = Date.now();
    const time_stamps = cooldowns.get(command.name);
    const cooldown_ammount = (command.cooldown) * 1000;

    if(time_stamps.has(message.author.id)){
        const expiration_time = time_stamps.get(message.author.id) + cooldown_ammount;

        if(current_time < expiration_time){
            const time_left = (expiration_time - current_time) / 1000;

            return message.reply(`Please wait ${time_left.toFixed(1)} more seconds before using ${command.name}`)
        }
    }

    time_stamps.set(message.author.id, current_time);
    setTimeout(() => time_stamps.delete(message.author.id), cooldown_ammount);

    try{
        command.execute(message,args, cmd, client, Discord);
    } catch (err){
        message.reply("There was an error trying to execute this command!");
        console.log(err);
    }
    
}

这是我的 ping.js

this is my ping.js

module.exports = {
    name: 'ping',
    cooldown: 10,
    permissions: ["SEND_MESSAGES"],
    description: "this is a ping command!",
    execute(client, message, args, cmd, Discord){
        message.channel.send('pong!');
        
    }
}

这是我的ban.js

module.exports = {
    name: 'ban',
    aliases: ['b'],
    permissions: ["ADMINISTRATOR", "BAN_MEMBERS",],
    description: "this is a ban command!",
    execute(client, message, args, cmd, Discord){
        const member = message.mentions.users.first();
        if(member){
            const memberTarget = message.guild.members.cache.get(member.id);
            memberTarget.ban();
            message.channel.send('User has been banned');
        }else{
            message.channel.send('Need to mention a member you wish to ban')
        }
       
    }
 }

推荐答案

这是一个非常明显的错误,你给出了错误的定义,你做了一个

This is a very obvious mistake where you have given the wrong definitions, you have done a

command.execute(message,args, cmd, client, Discord);

在你的命令文件中,你已经使用了

and inside of your command files, you have used

execute(client, message, args, cmd, Discord){

您可以通过在命令处理程序中使用以下内容来解决此问题:

You can fix this by using the following inside of your command handler:

command.execute(client, message, args, cmd, Discord);

希望对您有所帮助,如果您需要更多帮助,请在下方评论.

Hope this helps, if you need any more help, please comment below.

这篇关于TypeError: Cannot read property 'send' of undefined for my commands and a TypeError: Cannot read property 'users' of undefined for other commands的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆