You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何创建仅持有staffrole数组中任一角色可使用的命令?

Restricting a Command to Specific Roles

Hey there! Let's walk through how to restrict your command to only users with any of the roles in your array. I'll assume you're using Discord.js (the most common library for Discord bots) since you're working with role IDs.

Core Permission Check Logic

First, the key part: checking if a user has any of the roles in your staffrole array. We can use the some() method to quickly verify if any role ID in the user's role cache matches an ID in your array.

const staffrole = ['383874699941117952', '149622819158884353', '149622998180036608'];

// Reusable helper function to check permissions
function hasStaffRole(member) {
  return member.roles.cache.some(role => staffrole.includes(role.id));
}

Implementation for Prefix Commands

If you're using traditional prefix commands (like !staffcommand), here's how to integrate the check seamlessly:

client.on('messageCreate', async (message) => {
  // Ignore bot messages and messages without your prefix
  if (message.author.bot || !message.content.startsWith('!')) return;

  const args = message.content.slice(1).trim().split(/ +/);
  const command = args.shift().toLowerCase();

  // Target command: !staffonly
  if (command === 'staffonly') {
    // Make sure we're in a server (member is undefined in DMs)
    if (!message.member) {
      return message.reply('这个命令只能在服务器内使用!');
    }

    // Check if the user has permission
    if (!hasStaffRole(message.member)) {
      return message.reply('抱歉,你没有权限使用这个命令!');
    }

    // Your command's core logic goes here
    await message.reply('你有权限执行这个命令!');
  }
});

Implementation for Slash Commands

For Discord's modern slash commands (recommended for better user experience), here's how to add the permission check:

client.on('interactionCreate', async (interaction) => {
  if (!interaction.isChatInputCommand()) return;

  // Target command: /staffonly
  if (interaction.commandName === 'staffonly') {
    // Ensure we're in a server context
    if (!interaction.member) {
      return interaction.reply({ content: '这个命令只能在服务器内使用!', ephemeral: true });
    }

    // Check permission status
    if (!hasStaffRole(interaction.member)) {
      return interaction.reply({ content: '抱歉,你没有权限使用这个命令!', ephemeral: true });
    }

    // Your command's core logic goes here
    await interaction.reply('你有权限执行这个命令!');
  }
});

Extra Tips

  • DM Safety: Always check if member exists before accessing roles—users can't have roles in direct messages!
  • Dynamic Updates: If you need to adjust allowed roles without restarting the bot, consider storing the IDs in a database instead of a hardcoded array.
  • Slash Command Default Permissions: For a more permanent restriction, you can set default permissions when registering the slash command to only allow your target roles. The above check is better if you need flexible, dynamic control.

内容的提问来源于stack exchange,提问作者Braguetta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:46:56