主要代码:
const Discord = require("discord.js");
const client = new Discord.Client();
const prefix = ";";
client.once("ready", () => {
console.log("Online dziecino!");
client.user.setActivity("jebanie bydgoszczy", {
type: "STREAMING",
url: "https://www.twitch.tv/its_not_important",
});
});
client.on("message", (message) => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if (command === "kick") {
client.commands.get("kick").execute(message, args);
}
});
client.login("my token is here");
踢命令:
module.exports = {
name: "kick",
description: "This command kicks a member!",
execute(message, args) {
const target = message.mentions.users.first();
if (target) {
const memberTarget = message.guild.members.cache.get(target.id);
memberTarget.kick();
message.channel.send("User has been kicked");
} else {
message.channel.send('You coudn\'t kick that member!');
}
},
};
错误:
TypeError: Cannot read property 'get' of undefined
at Client.<anonymous> (C:\Users\kubak\Desktop\Discord Bot\main.js:82:18)
at Client.emit (events.js:315:20)
答案 0 :(得分:1)
您需要命令处理程序:
将此添加到您的代码中:
client.commands= new Discord.Collection();
const commandFiles = readdirSync(join(__dirname, "commands")).filter(file => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(join(__dirname, "commands", `${file}`));
client.commands.set(command.name, command);
}
并在“令牌”之前添加:
client.on("message", async message => {
if(message.author.bot) return;
if(message.channel.type === 'dm') return;
if(message.content.startsWith(prefix)) {
const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if(!client.commands.has(command)) return;
try {
client.commands.get(command).run(client, message, args);
} catch (error){
console.error(error);
}
}
})
将名为“commands”的文件夹添加到您的机器人目录。 将 kick 命令放在命令文件夹中。
const Discord = require('discord.js');
module.exports = {
name: "kick",
description: "This command kicks a member!",
async run (client, message, args) {
const target = message.mentions.members.first() || message.guild.members.cache.get(args[0]);
if(!args[0]) return message.channel.send('Error, write target');
target.kick(reason)
.catch(err => {
if(err) return message.channel.send('Error.')
})
}
}