尝试在不和谐的bot上实现仅Patreon的命令/功能,我将如何实现? [英] Trying to implement Patreon-only commands/functions on my discord bot, how would I accomplish this?

查看:60
本文介绍了尝试在不和谐的bot上实现仅Patreon的命令/功能,我将如何实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的discord机器人将"Patreon"角色提供给我的patreon支持者.这个角色是在我的主要不和谐bot服务器上指定的.因此,现在我正在尝试编写一些命令,这些命令仅对在BOTS不和谐服务器中具有"Patreon"角色的用户可用.

就像有一种我可以成为的方式-

message.member.has('Patreon Role').in('My Discord Server)?

解决方案

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

  1. 与您的用户和相应的Patreon角色一起获得家庭公会".
    请参见 Client.guilds Map.get() .

  2. 在公会中查找用户.
    请参见 Guild.member() .

  3. 检查用户是否具有Patreon角色.
    请参见 GuildMember.roles Collection.find() .

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

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

 //假设客户端"是Discord Client的实例.函数isSupporter(user){const homeGuild = client.guilds.get('idHere');如果(!homeGuild)返回console.error('找不到机器人公会!');const member = homeGuild.member(user);如果(!member)返回false;const role = member.roles.find(role => role.name ==='Patreon');如果(!role)返回false;返回true;} 

然后,例如,在命令中使用此功能...

 //假设消息"是一条消息.如果(!isSupporter(message.author)){return message.channel.send(':x:此命令仅限Patreon支持者.').catch(console.error);} 

My discord bot gives the role of 'Patreon' to my patreon supporters. This role is given on my main discord bot server. So right now I'm trying to write some commands that would be only available to users who have the role 'Patreon' in the BOTS discord server, how can I accomplish this?

Like is there a way I can be like -

message.member.has('Patreon Role').in('My Discord Server)?

解决方案

Let's go over the tasks you need to accomplish this.

  1. Get the "home guild" with your users and corresponding Patreon role.
    See Client.guilds and Map.get().

  2. Find the user in the guild.
    See Guild.member().

  3. Check whether or not the user has the Patreon role.
    See GuildMember.roles and Collection.find().

You can define a function to help you out with this, export it and require it where you need it (or define it within relevant scope), and then call it to check if a user is one of your Patreon supporters.

Here's what this function would look like...

// 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;
}

Then, as an example, using this function in a command...

// Assuming 'message' is a Message.

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

这篇关于尝试在不和谐的bot上实现仅Patreon的命令/功能,我将如何实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆