简体   繁体   中英

Discord.py | A simple command to give a specific user a specific role?

I'm working on a bot that can give roles to users, but I'm not very well-versed in the discord.py syntax.

I've tried adapting code like this:

  if message.content.startswith('!!role'):
   member = message.author
   var = discord.utils.get(message.guild.roles, name = "Role A")
   member.add_role(var)

But this code doesn't fit my purpose, I'm trying to get a function more like this: roleGive(member,role) so then I could have a command in discord like !role @JohnSmith#0001 moderator , and it would give the user JohnSmith the 'moderator' role. Also, this function is going to be used elsewhere in the bot, so I'm trying to make the function reusable, so I could do something like (this is more-or-less pseudocode, I'm still learning):

user = "JohnSmith#0001"

if 20 > user.messagecount() >= 10:
    roleGive(user,"10 Messages Gang")

if 30 > user.messagecount() >= 20:
    roleGive(user,"20 Messages Gang")

I also do want to stress that I have looked at a lot of similar questions on this site, and either the code is outdated and no longer works, or it does not fit my purpose. Thanks for any help!

Try this for the function!

#import discord
async def roleGive(member: discord.Member, role: discord.Role):
    await member.add_roles(role)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM