在使用Discord.js-commando时,要检查一个被提到的用户是否拥有某个特定角色(例如静音角色),你可以按照以下步骤进行:
以下是一个示例代码,展示了如何检查提到的用户是否拥有静音角色:
const { CommandoClient } = require('discord.js-commando');
const { GatewayIntentBits } = require('discord-api-types/v9');
const client = new CommandoClient({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildRoleTags
]
});
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.registry.registerGroup('admin', 'Admin commands');
client.registry.registerGroup('misc', 'Miscellaneous commands');
client.registry.registerDefaults();
client.registry.registerCommand('checkrole', {
name: 'checkrole',
group: 'admin',
memberPermissions: ['MANAGE_ROLES'],
description: 'Check if a mentioned user has a specific role.',
args: [
{
key: 'user',
type: 'member',
prompt: 'Please mention a user.'
}
],
async run(message, { user }) {
const muteRole = message.guild.roles.cache.find(role => role.name === 'Mute');
if (!muteRole) {
return message.reply('The Mute role does not exist.');
}
const hasMuteRole = user.roles.cache.has(muteRole.id);
if (hasMuteRole) {
return message.reply(`${user} has the Mute role.`);
} else {
return message.reply(`${user} does not have the Mute role.`);
}
}
});
client.login('YOUR_BOT_TOKEN');
args
中的user
参数获取被提到的用户。message.guild.roles.cache.find
方法查找名为Mute
的角色。user.roles.cache.has
方法检查用户是否拥有该角色。通过以上步骤和示例代码,你可以轻松地检查提到的用户是否拥有特定角色,例如静音角色。
领取专属 10元无门槛券
手把手带您无忧上云