无法读取未定义的属性(读取' get')discord.js
因此,我最近开始使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您应该检查: https:// discordjs:// 。
他知道,
GET
根本存在。您必须编写一些代码来创建和设置这些命令,从doc中示出:
您可以以这种方式或其他方式执行,但是目前您的对象为空。
即使您的
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 :
You can do it that way or an other, but at this time your object is empty.
Even your
client.commands
is undefined !