我试图通过在达到某个级别时自动将用户添加到角色来向我的不和谐机器人xp-system
添加功能。
我一直在尝试所有我能想到的解决方案,但似乎没有一个起作用。
尝试过:
addrankup.addRole(fullmember).catch(console.error);
let addrankup.addRole(fullmember).catch(console.error);
addrankup.addRole(fullmember.id).catch(console.error);
当用户达到设定水平以自动获得排名时预期:
let xpAdd = Math.floor(Math.random() * 5) + 10;
console.log(xpAdd);
if (!xp[message.author.username]) {
xp[message.author.username] = {
xp: 0,
level: 1
};
}
let curxp = xp[message.author.username].xp;
let curlvl = xp[message.author.username].level;
let nxtLvl = xp[message.author.username].level * 200;
xp[message.author.username].xp = curxp + xpAdd;
if (nxtLvl <= xp[message.author.username].xp) {
xp[message.author.username].level = curlvl + 1;
message.channel.send("Level Up!").then(msg => {
msg.delete(5000)
});
}
if (curlvl <= xp[message.author.username].level) {
xp[message.author.username].level = "2";
let addrankup = message.mentions.members.first();
let fullmember = message.guild.roles.find(`name`, "Full Member");
addrankup.addRole(fullmember.id).catch(console.error);
message.channel.send("You have received a new role!").then(msg => {
msg.delete(5000)
});
}
fs.writeFile("./xp.json", JSON.stringify(xp), (err) => {
if (err) console.log(err)
});
但这是控制台告诉我的:
(node:8460) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'addRole' of undefined
at Client.bot.on (E:\#DeathKillerNOR Discord Bots\DeathKillerBot\index.js:125:15)
at Client.emit (events.js:187:15)
at MessageCreateHandler.handle (E:\#DeathKillerNOR Discord Bots\DeathKillerBot\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
at WebSocketPacketManager.handle (E:\#DeathKillerNOR Discord Bots\DeathKillerBot\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:103:65)
at WebSocketConnection.onPacket (E:\#DeathKillerNOR Discord Bots\DeathKillerBot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
at WebSocketConnection.onMessage (E:\#DeathKillerNOR Discord Bots\DeathKillerBot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17)
at WebSocket.onMessage (E:\#DeathKillerNOR Discord Bots\DeathKillerBot\node_modules\ws\lib\event-target.js:120:16)
at WebSocket.emit (events.js:182:13)
at Receiver._receiver.onmessage (E:\#DeathKillerNOR Discord Bots\DeathKillerBot\node_modules\ws\lib\websocket.js:137:47)
at Receiver.dataMessage (E:\#DeathKillerNOR Discord Bots\DeathKillerBot\node_modules\ws\lib\receiver.js:409:14)
(node:8460) 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(). (rejection id: 1)
(node:8460) [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.
答案 0 :(得分:1)
该错误告诉您addrankup
未定义,这反过来意味着message.mentions.members.first();
不返回任何内容。我的猜测是,而不是
let addrankup = message.mentions.members.first();
您正在寻找
let addrankup = message.member;