我正在使用discord.js尝试使机器人成为一个机器人,其唯一目的是每当成员被赋予特定角色(alpha,bravo,charlie,delta)时,它将向特定频道发送公告(#general)恭喜您加入了该派系!我将如何去做呢? (我知道这是“ GuildMembers”块的一部分)
interface Data {
id: number
}
function typescriptType(param: Data | number): void {
let data;
if (typeof param === object) {
data = param.id;
} else {
data = param;
}
}
typescriptType({id : 1});
typescriptType(1);
不幸的是,此错误返回:
const Discord = require('discord.js');
const client = new Discord.Client();
const token = 'NzUzNTY0NjA0MjM1MzgyODI1.X1oBug.E0GEmnakRtQMbOfg5IwllQsW_Ps';
client.on('ready', () => {
console.log('This bot is online!')
})
client.on("guildMemberUpdate", async(oldMember, newMember) => {
// On `guildMemberUpdate`
if (oldMember.roles.cache !== newMember.roles.cache) {
// Check if a role was updated
let newRole;
newMember.roles.cache.forEach((role) => {
if (oldMember.roles.cache.includes(role)) return;
// Check for the new role that was added
let roleNames = ["bobbies", "beebos", "babbos", "bobbios"];
if (roleNames.toLowerCase().includes(role.name.toLowerCase())) {
// Check for only `['bobbies', 'beebos', 'babbos', 'bobbios']`
newRole = role;
}
});
// Anything you want to run here with the `newRole` data.
const channel = oldMember.guild.channels.cache.find(
(channel) => channel.name === "general"
);
channel.send("Congratulations on becoming part of the faction!");
}
});
client.login(token);
此错误是由于在没有catch块的情况下抛出异步函数引起的,或者是由于拒绝了(node:6240) UnhandledPromiseRejectionWarning: TypeError: oldMember.roles.cache.includes is not a function
at C:\Users\sanderj\Desktop\Discord Bot\index.js:18:39
at Map.forEach (<anonymous>)
at Client.<anonymous> (C:\Users\sanderj\Desktop\Discord Bot\index.js:17:31)
at Client.emit (events.js:315:20)
at Object.module.exports [as GUILD_MEMBER_UPDATE] (C:\Users\sanderj\Desktop\Discord Bot\node_modules\discord.js\src\client\websocket\handlers\GUILD_MEMBER_UPDATE.js:25:16)
at WebSocketManager.handlePacket (C:\Users\sanderj\Desktop\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
at WebSocketShard.onPacket (C:\Users\sanderj\Desktop\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
at WebSocketShard.onMessage (C:\Users\sanderj\Desktop\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
at WebSocket.onMessage (C:\Users\sanderj\Desktop\Discord Bot\node_modules\ws\lib\event-target.js:125:16)
at WebSocket.emit (events.js:315:20)
(node:6240) UnhandledPromiseRejectionWarning: Unhandled promise rejection.
未处理的承诺而导致的。要在未处理的承诺拒绝时终止节点进程,请使用CLI标志.catch()
(请参见https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode)。 (拒绝ID:1)
(节点:6240)[DEP0018] DeprecationWarning:已弃用未处理的承诺拒绝。将来,未处理的承诺拒绝将以非零退出代码终止Node.js进程。
答案 0 :(得分:0)
检出Client#guildMemberUpdate
,它会在db.getCollectionNames().someFunction()
[
"abc_1",
"abc_2",
"abc_3"
]
属性的某些内容发生更改(即添加角色)时发出。尝试将以下代码添加到您的机器人中:
member