无法读取未定义的属性(读取' get')discord.js

发布于 2025-02-08 16:26:16 字数 1630 浏览 3 评论 0原文

因此,我最近开始使用discord.js开发一个Discord机器人,当我尝试运行此命令时,当我在执行命令的行中更新bot时,它会引起错误( client.commands.get('king')。执行(消息,args);)。

错误是:

TypeError: Cannot read properties of undefined (reading 'get'
    at Client.<anonymous> (C:\Users\Shushan\Desktop\discordbot\main.js:18:25)
    at Client.emit (node:events:527:28) )

这是我的代码文件:

main.js

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

const client = new Discord.Client();

const prefix = '?';

client.once('ready', () => {
    console.log("Shushbot 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 === 'kick') {
        client.commands.get('kick').execute(message, args);
    }
})

client.login('SECURITY TOKEN');

kick.js

module.exports = {
    name: 'kick',
    description: 'This Command Kicks People!',
    execute(message, args) {
        const member = message.mentions.users.first();
        if(member) {
            const memberTarget = message.guild.members.cache.get(member.id);
            memberTarget.kick();
            message.channel.send("This User Has Been Kicked From The Server.");
        } else {
            message.channel.send("This User Couldn't Be Kicked.");
        }
    }
}

So I recently started developing a discord bot using discord.js, and when I'm trying to run this command, it is raising an error when I update the bot at the line where I execute the command (client.commands.get('kick').execute(message, args);).

The error is:

TypeError: Cannot read properties of undefined (reading 'get'
    at Client.<anonymous> (C:\Users\Shushan\Desktop\discordbot\main.js:18:25)
    at Client.emit (node:events:527:28) )

And here are my code files:

main.js

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

const client = new Discord.Client();

const prefix = '?';

client.once('ready', () => {
    console.log("Shushbot 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 === 'kick') {
        client.commands.get('kick').execute(message, args);
    }
})

client.login('SECURITY TOKEN');

kick.js

module.exports = {
    name: 'kick',
    description: 'This Command Kicks People!',
    execute(message, args) {
        const member = message.mentions.users.first();
        if(member) {
            const memberTarget = message.guild.members.cache.get(member.id);
            memberTarget.kick();
            message.channel.send("This User Has Been Kicked From The Server.");
        } else {
            message.channel.send("This User Couldn't Be Kicked.");
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

皓月长歌 2025-02-15 16:26:16

我认为您应该检查: https:// discordjs:// 。

​他知道,GET根本存在。

您必须编写一些代码来创建和设置这些命令,从doc中示出:


client.commands = new Collection();
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const filePath = path.join(commandsPath, file);
    const command = require(filePath);
    // Set a new item in the Collection
    // With the key as the command name and the value as the exported module
    client.commands.set(command.data.name, command);
}

您可以以这种方式或其他方式执行,但是目前您的对象为空。

即使您的client.commands也不确定!

I think you should check that : https://discordjs.guide/creating-your-bot/command-handling.html#reading-command-files

So far, your bot (client) don't know the commands method, and even if he knows, get dosent exists at all.

You have to write some code to create and set these commands, exemple from doc :


client.commands = new Collection();
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const filePath = path.join(commandsPath, file);
    const command = require(filePath);
    // Set a new item in the Collection
    // With the key as the command name and the value as the exported module
    client.commands.set(command.data.name, command);
}

You can do it that way or an other, but at this time your object is empty.

Even your client.commands is undefined !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文