Trying to implement Patreon-only commands/functions on my discord bot, how would I accomplish this?

試著忘記壹切 提交于 2021-01-29 10:57:09

问题


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)?


回答1:


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



回答2:


message.member.roles.find('name', 'Patreon Role');//this returns either undefined or a role

What that does is it searches the users collection to see if the have "Patreon Role"

If the message is on the same server, otherwise you could do

client.guild.find('name','My Discord Server').member(message.author).roles.find('name', 'Patreon Role'); //this also returns undefined or a role

Clearly that second option is long, but what is basically does is searches the servers the bot is in for a server called 'My Discord Server' then it finds the GuildMember form of the message.author user resolvable, then it searches their roles for the role 'Patreon Role'

There is a chance it will crash though if they aren't on the server(the documentation doesn't say if it returns and error or undefined for some reason) so if it does crash you could instead do

  client.guild.find('name','My Discord Server').members.find('id', message.author.id).roles.find('name', 'Patreon Role'); //this also returns undefined or a role

You can read more here: https://discord.js.org/#/docs/main/stable/class/User

and here

https://discord.js.org/#/docs/main/stable/class/Client

and here

https://discord.js.org/#/docs/main/stable/class/Guild




回答3:


To try and give a full example, assuming this is in your message event

if (message.member.roles.find(r => r.name === 'Patreon') == undefined &&
    commandIsExclusive || message.guild.id !== 'this-id-for-BOTS-server') {
  // Don't allow them in here
}

Essentially, to run a command they must be a supporter, in a specific server and if it is exclusive and the other criteria aren't met, they are denied



来源:https://stackoverflow.com/questions/56677081/trying-to-implement-patreon-only-commands-functions-on-my-discord-bot-how-would

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!