我用discord.js开发了一个使用msg.member.hasPermission("ADMINISTRATOR")
或msg.member.roles.cache.has(teacherRoleID)
之类的机器人。一切正常,直到我尝试了webhooks。通过添加这两行:
client.on('ready', () => {
client.user.setStatus("online")
client.user.setActivity("!help", {
type: "PLAYING",
});
superConsole(`Logged in as ${client.user.tag} in ${client.guilds.size} guilds!`);
const hook = new Discord.WebhookClient("ID", "secret token"); // THESE
hook.send("I am now alive!"); // LINES
});
(顺便说一句 superConsole 是一个函数)
从那时起,该程序不再起作用,并始终返回相同的错误:(node:20736) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'hasPermission' of null
和(node:20736) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'roles' of null
当我删除Webhook的这两行时,它再次起作用。为什么?我不明白。
权限和角色事物在消息侦听器中:
client.on('message', async msg => {
if (msg.member.hasPermission('ADMINISTRATOR') {
// some stuff here
}
if (msg.member.roles.cache.has(teacherRoleID) {
// some stuff here
}
});
答案 0 :(得分:0)
问题在于,当您从hook
发送消息时,它将触发客户端的message
事件。由于Webhook不是公会成员,因此msg.member
将从Webhook发送的消息中获得undefined
。
您将必须使用以下内容:
if (msg.member) {
if (msg.member.hasPermission('ADMINISTRATOR') {
// some stuff here
}
if (msg.member.roles.cache.has(teacherRoleID) {
// some stuff here
}
}