首页 > 解决方案 > 如何在 Discord.js 中获取用户询问命令的角色

问题描述

我试图找出命令(whotohelp)的用户是否具有某些角色('Lab Demonstrator`),但是,我似乎不明白如何获得该角色。

对于我的“interactionCreate”,我目前有以下内容:

client.on('interactionCreate', async interaction => {

  if (!interaction.isCommand()) return;
  (interaction.commandName);
  //gets username
  var username = (interaction.user.username);
  //test code
  if (interaction.commandName === 'ping') {
  
    await interaction.reply('Pong!');
  }

在 Python@commands.has_any_role("Lab Demonstrator","lab demonstrator", "staff")中,我使用如下:

@client.command(pass_context=True)
@commands.has_any_role("Lab Demonstrator","lab demonstrator", "staff","teaching-assistant","lecturers","admin", "Admin")
async def whoToHelp(mesage):

什么是 Javascript 替代方案?

标签: discorddiscord.js

解决方案


您可以Collection.some用于布尔值和Collection.find角色对象。

布尔值

const hasRole = interaction.member.roles.cache.some(r => r.name === "the_role_name")
//true if they have it, false if they don't

角色对象

const role = interaction.member.roles.cache.find(r => r.name === "the_role_name")
//the role object if they have it

推荐阅读