我目前正在制作一个带有许多不同命令的不和谐机器人,在实现了一个 ?beg 和一个 ?bal 命令来乞求我的假想货币“比特”之后,它似乎破坏了很多代码。我能够修复所有问题,除了一个错误,它来自于输入 ?verify。当您键入 ?verify 时,机器人会向您发送 ?verify 的聊天发送一个嵌入,并要求成员对嵌入做出反应,并打勾以赋予“会员”角色。在键入 ?verify 并按 Enter 后,会出现嵌入,并且机器人也会对自身做出反应,并带有刻度,尽管在做出反应时,成员不会获得角色。当我查看终端时,出现了这个错误,
(node:44564) UnhandledPromiseRejectionWarning: TypeError: client.on is not a function
at Object.execute (C:\Users\013933\Desktop\Vixe\commands\verify.js:20:16)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
(node:44564) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:44564) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
这很奇怪,因为 client.on 是在代码顶部定义的函数。
async execute(message, client, args, Discord) {
我已经在堆栈溢出时对其进行了搜索,但只是人们说我错误地声明了“客户端”,尽管当“正确”声明客户端时,我只是得到另一个错误,说“客户端”已经定义了它是什么。
这是完整的代码,
module.exports = {
name: 'verify',
description: 'Allows members to react to a message to verify themselves.',
async execute(message, client, args, Discord) {
const channel = message.channel.id;
const memberRole = message.guild.roles.cache.find(role => role.name === 'Member');
const memberEmoji = '✅';
const { MessageEmbed } = require('discord.js');
let embed = new MessageEmbed()
.setColor('#800080')
.setTitle('Verification')
.setDescription('React to this embed with the :white_check_mark: to verify yourself and gain access to the server.\n'
+ `Removing your reaction to this embed will un-verify you.`);
let messageEmbed = await message.channel.send(embed);
messageEmbed.react(memberEmoji);
client.on('messageReactionAdd', async (reaction, user) => {
if (reaction.message.partial) await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();
if (user.bot) return;
if (!reaction.message.guild) return;
if (reaction.message.channel.id == channel) {
if (reaction.emoji.name === memberEmoji) {
await reaction.message.guild.members.cache.get(user.id).roles.add(memberRole);
}
} else {
return;
}
});
client.on('messageReactionRemove', async (reaction, user) => {
if (reaction.message.partial) await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();
if (user.bot) return;
if (!reaction.message.guild) return;
if (reaction.message.channel.id == channel) {
if (reaction.emoji.name === memberEmoji) {
await reaction.message.guild.members.cache.get(user.id).roles.remove(memberRole);
}
} else {
return;
}
});
}
}
这是处理所有命令的 message.js,
const profileModel = require('../../models/profileSchema');
module.exports = async (Discord, client, message) => {
const prefix = '?';
if (!message.content.startsWith(prefix) || message.author.bot) return;
let profileData;
try {
profileData = await profileModel.findOne({ userID: message.author.id });
if(!profileData) {
let profile = await profileModel.create({
userID: message.author.id,
serverID: message.guild.id,
bits: 1000,
bank: 0,
});
profile.save();
}
} catch (err) {
console.log(err);
}
const args = message.content.slice(prefix.length).split(/ +/);
const cmd = args.shift().toLowerCase();
const command = client.commands.get(cmd);
try {
command.execute(message, args, cmd, client, Discord, profileData);
} catch (err) {
message.reply('There was an error executing this command.');
console.log(err);
}
};