无法读取 null 的属性(读取 'me')(DISCORD JS)
Cannot read properties of null (reading 'me') (DISCORD JS)
client.on('message', async message => {
let guild = message.guild
if (!guild.me.hasPermission("ADMINISTRATOR")) return
})
当我的机器人加入服务器时,出现这个错误
if (!guild.me.hasPermission("ADMINISTRATOR")) return
^
TypeError: Cannot read properties of null (reading 'me')
at Client.<anonymous> (/home/runner/Todoroki/index.js:236:16)```
没有太多信息,我只能推断me
是null
。要解决它,您可以使用 Optional Chaining 检查 guild.me
是否为 truthy
,如下所示:
if (!guild?.me?.hasPermission ...
其实我知道怎么做了。我刚刚添加:
let guild = message.guild
if (!guild) return;
// rest of commands
这意味着该命令在服务器上运行。或者您没有启用 GUILDS 意图。简直
client.on('message',message =>{
if(!message.guild) return;
// Do whatever you want
})
client.on('message', async message => {
let guild = message.guild
if (!guild.me.hasPermission("ADMINISTRATOR")) return
})
当我的机器人加入服务器时,出现这个错误
if (!guild.me.hasPermission("ADMINISTRATOR")) return
^
TypeError: Cannot read properties of null (reading 'me')
at Client.<anonymous> (/home/runner/Todoroki/index.js:236:16)```
没有太多信息,我只能推断me
是null
。要解决它,您可以使用 Optional Chaining 检查 guild.me
是否为 truthy
,如下所示:
if (!guild?.me?.hasPermission ...
其实我知道怎么做了。我刚刚添加:
let guild = message.guild
if (!guild) return;
// rest of commands
这意味着该命令在服务器上运行。或者您没有启用 GUILDS 意图。简直
client.on('message',message =>{
if(!message.guild) return;
// Do whatever you want
})