首页 > 解决方案 > 尝试在我的不和谐机器人上实现仅 Patreon 的命令/功能,我将如何实现这一点?

问题描述

我的不和谐机器人将“Patreon”的角色赋予了我的 patreon 支持者。这个角色在我的主要不和谐机器人服务器上给出。所以现在我正在尝试编写一些仅对在 BOTS 不和谐服务器中具有“Patreon”角色的用户可用的命令,我该如何完成呢?

有没有办法让我像 -

message.member.has('Patreon Role').in('我的 Discord 服务器)?

标签: discord.js

解决方案


让我们回顾一下完成此任务所需的任务。

  1. 与您的用户和相应的 Patreon 角色一起获得“家庭公会”。
    Client.guildsMap.get()

  2. 在公会中找到用户。
    Guild.member()

  3. 检查用户是否具有 Patreon 角色。
    GuildMember.rolesCollection.find()

您可以定义一个函数来帮助您解决此问题,将其导出并在您需要的地方使用它(或在相关范围内定义它),然后调用它来检查用户是否是您的 Patreon 支持者之一。

这是这个函数的样子......

// Assuming 'client' is the instance of your Discord Client.

function isSupporter(user) {
  const homeGuild = client.guilds.get('idHere');
  if (!homeGuild) return console.error('Couldn\'t find the bots guild!');

  const member = homeGuild.member(user);
  if (!member) return false;

  const role = member.roles.find(role => role.name === 'Patreon');
  if (!role) return false;

  return true;
}

然后,作为一个例子,在命令中使用这个函数......

// Assuming 'message' is a Message.

if (!isSupporter(message.author)) {
  return message.channel.send(':x: This command is restricted to Patreon supporters.')
    .catch(console.error);
}

推荐阅读