guild.member.hasPermission不是一个函数



我从v11翻找了一个旧的discord.js源,并将其大部分合并到v12,请注意,这个脚本在ready事件中是错误的,我宁愿不把它放在其他地方。

这是我现在的内容:

client.on('ready', async () => {
log.success(`Authenticated as ${client.user.tag}`);
client.user.setPresence({
activity: {
name: env.PRESENCE_ACTIVITY,
type: env.PRESENCE_TYPE.toUpperCase(),
},
});
const guild = client.guilds.cache.find((g) => g.id === '000000000');
if (!guild)
return console.log(`Can't find any guild with the ID "${env.GUILD_ID}"`);
if (guild.member.hasPermission('ADMINISTRATOR', false)) {
log.success("Bot has the 'ADMINISTRATOR' permission");
} else 
log.warn("Bot does not have 'ADMINISTRATOR' permission");
client.guilds.cache
.get(env.GUILD_ID)
.roles.fetch()
.then((roles) => {
goodieRole = roles.cache.get(env.GOODIE_ROLE_ID);
});
});

guilds上没有member属性。如果你想把bot作为guild的成员,你可以从guild.membersfetch()它们。如果您使用的是discord.js v12,您可以使用hasPermission()方法来检查bot是否有ADMINISTRATOR标志:

client.on('ready', async () => {
log.success(`Authenticated as ${client.user.tag}`);
client.user.setPresence({
activity: {
name: env.PRESENCE_ACTIVITY,
type: env.PRESENCE_TYPE.toUpperCase(),
},
});
const guild = client.guilds.cache.get(env.GUILD_ID);
if (!guild)
return console.log(`Can't find any guild with the ID "${env.GUILD_ID}"`);
let botMember = await guild.members.fetch(client.user);
if (botMember.hasPermission('ADMINISTRATOR', false)) {
log.success("Bot has the 'ADMINISTRATOR' permission");
} else
log.warn("Bot does not have 'ADMINISTRATOR' permission");

// ...

最新更新