所以我在另一个文件中用module.exports等创建了这个kick命令。
我的问题是,我如何在index.js中运行该文件,这样当我在Discord中键入命令时,它实际上会踢人。
module.exports = {
name: 'kick',
description: 'Kick a member',
userPermissions: ["KICK_MEMBERS"],
options: [
{
name: 'target',
description: 'target to kick',
type: 'USER',
required: true
},
{
name: 'reason',
description: 'reason for this kick',
type: 'STRING',
required: false
}
],
run: async(client, interaction, args) => {
const target = interaction.options.getMember('target');
const reason =
interaction.options.getString('reason') || "No reason provided";
if(
target.roles.highest.position >=
interaction.member.roles.highest.position
)
return interaction.followUp({
content:
'You can not take action on this user as their role is heigher than yours',
});
await target.send(
`You have been kicked from ${interaction.guild.name}, reason: ${reason}`
);
target.kick(reason);
interaction.followUp({
content: `Kicked ${target.user.tag} successfully! reason: ${reason}`,
});
},
}
这就是我用来在index.js<中运行它的方法这不起作用。它适用于ping命令等,但不适用于ban或kick。
client.on("messageCreate", (message) => {
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase()
if(command == 'help'){
client.commands.get('help').execute(message)
}
if(command == 'ping'){
client.commands.get('ping').execute(message)
}
if(command == 'kick'){
client.commands.get('kick').execute(message)
}
if(command == 'ban'){
client.commands.get('ban').execute(message)
}
})
有人能帮我吗?我会非常感激
您已经将kick命令设置为用作斜杠命令(交互(,但它正在侦听messageCreate
而不是interactionCreate
中的命令。
假设您的机器人被配置为将命令存储在映射中,您需要让它侦听client.on('interactionCreate'(事件下的命令。
将此代码添加到事件侦听器:
if (interaction.isCommand()) {
const command = client.commands.get(interaction.commandName);
if (!command) {
return interaction.reply({
content: "An error has occurred ",
ephemeral: true
}).catch((error) => client.utils.log.handler("error", error));
}
const args = {};
for (const option of interaction.options.data) {
if (option.type === "SUB_COMMAND") {
if (option.name) args[option.name] = true;
option.options?.forEach((x) => {
args[x.name] = x.value;
});
} else if (option.value) {
args[option.name] = option.value;
}
}
interaction.member = interaction.guild.members.cache.get(interaction.user.id);
try {
command.run(client, interaction, args);
} catch (error) {
client.utils.log.error(error);
interaction.reply({
content: "There was an error while executing this command!",
ephemeral: true
}).catch((err) => client.utils.log.handler("error", err));
}
}
如果您没有映射命令,请在侦听器事件之外使用此代码映射它们-将其添加到client.login(token)
之上
改变/命令到存储命令的任何位置。
const fs = require('fs')
client.commands = new Collection()
const commandList = []
const commandFiles = fs.readdirSync(`./commands`).filter(file => file.endsWith('.js'))
for (const file of commandFiles) {
const command = require(`./commands/${file}`)
commandList.push(` ${file.split('.')[0]}`)
client.commands.set(command.data.name, command)
}
console.log(`${commandFiles.length} commands loaded. ${commandList}`)