我在warn.js 中的命令
module.exports = {
name: 'warn',
commands: 2,
expectedArgs: "<Target user's @> <reason>",
requiredRoles: ['Admin'],
callback: (message, arguments) => {
const target = message.mentions.users.first()
if (!target) {
message.reply('Please specify a MEMEBR to warn!!')
return
}
arguments.shift()
const guildId = message.guild.id
const userId = message.member.id
const reason = arguments.join(' ')
console.log(guildId, userId, reason)
},
}
我在 index.js 中的代码
else if (command == 'warn'){
client.commands.get('warn').execute(message, args, Discord, client);
}
我的错误
/home/runner/discord-quack-admin/index.js:85
client.commands.get('warn').execute(message, args, Discord, client);
^
TypeError: client.commands.get(...).execute is not a function
at Client.<anonymous> (/home/runner/discord-quack-admin/index.js:85:35)
at Client.emit (events.js:314:20)
at Client.EventEmitter.emit (domain.js:483:12)
at MessageCreateAction.handle (/home/runner/discord-quack-admin/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (/home/runner/discord-quack-admin/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (/home/runner/discord-quack-admin/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
at WebSocketShard.onPacket (/home/runner/discord-quack-admin/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
at WebSocketShard.onMessage (/home/runner/discord-quack-admin/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
at WebSocket.onMessage (/home/runner/discord-quack-admin/node_modules/ws/lib/event-target.js:132:16)
我对 DISCORD.JS 非常陌生,所以请解释我该怎么做,如果可能,请重新编写代码以帮助我
答案 0 :(得分:0)
这是我使用的警告命令。我目前没有将警告保存在日志中,但可以轻松添加。它确实在 .json 文件中存储了用户已被警告的信息。在 5 警告时,用户将在默认时间内自动静音。您需要更改的是静音角色,以及机器人将在哪里发送操作日志。
module.exports = {
name: 'warn',
aliases: ['warn'],
category: 'staff',
utilisation: '{prefix}help <command name>',
execute(client, message, args) {
const Discord = require("discord.js");
const mutedroleid = '818147666785337406';
const fs = require('fs');
const mutedrole = message.guild.roles.cache.get(mutedroleid);
const warns = require("./warns.json");
if(!message.member.hasPermission("MUTE_MEMBERS")) return message.send("I cannot let you do that");
const user = message.mentions.users.first();
if (!user) return message.send("Please specify someone you want to warn. **&warn <user> [reason]**");
const target = message.guild.members.cache.get(user.id);
if(target.roles.cache.has(mutedroleid)) return message.reply("You cannot warn muted members.");
if(!mutedrole) return message.send("Couldn't find the Muted role.");
message.delete()
const reason = args.slice(1).join(" ");
if(!warns[user.id]) {
warns[user.id] = {
warnCount: 1
}
} else {
warns[user.id].warnCount += 1;
}
if(warns[user.id].warnCount >= 5) {
const mute = new Discord.MessageEmbed()
.setColor("#ff7b23")
.setDescription(`${user} has been muted. (**5**/**5**)\nReason: **${reason != "" ? reason : "-"}**`);
message.channel.send(mute);
message.guild.members.cache.get(user.id).send(mute)
let botlog = message.guild.channels.cache.find(x => x.name === "bot-logs");
botlog.send(mute);
target.roles.add(mutedrole.id);
warns[user.id].warnCount = 0;
setTimeout(() => {
target.roles.remove(mutedrole.id);
const unmute = new Discord.MessageEmbed()
.setColor("#23ff82")
.setDescription(`${user} has been unmuted.`);
message.channel.send(unmute);
message.guild.members.cache.get(user.id).send(unmute)
}, 1000 * 900);
} else {
const warn = new Discord.MessageEmbed()
.setColor("#ffd641")
.setDescription(`${user} has been warned by ${message.author}. (**${warns[user.id].warnCount}**/**5**) \nReason: **${reason != "" ? reason : "-"}**`);
message.channel.send(warn);
message.guild.members.cache.get(user.id).send(warn)
let botlog = message.guild.channels.cache.find(x => x.name === "bot-logs");
botlog.send(warn);
}
fs.writeFile("./warns.json", JSON.stringify(warns), err => {
if (err) console.log(err);
});
},
};
.json 文件格式。将此文件保存在机器人的主文件夹中。有时它会创建文件有时它不会。
{}
答案 1 :(得分:0)
这里的问题是您混合使用了其他命令处理程序。
在这样的系统中,一切都必须保持一致。
您的命令定义了 callback
,但您调用了 execute
。
您也不能使用 arguments
作为参数(或者至少,您不应该使用)。
固定定义:
execute: (message, args, Discord, client) => {
您的代码仍然会出错。不要在不知道它做什么的情况下从随机位置复制代码,因为它会导致像这样的混乱。 I'd suggest to learn the basics of JS first