0

I have a discord bot where you can activate your key with !activate. At the event i want the bot to give the message.author the PREMIUM MEMBER role at my discord. How can i make that work

I'm using @client.event and not ctx.

Thanks for your answers!

1

2 Answers 2

0

Try this

def check_activation_key(activation_key):
    # do your check here, return True or False

@client.event
async def on_message(message):

    if message.content.startswith('!activate ') and message.channel == message.author.dm_channel: # !activate, dms only
        activation_key = message.content[10:]
        if check_activation_key(activation_key):
            guild = client.get_guild(ID_OF_SERVER_TO_ASSIGN_ROLE_IN) # right click and "copy id"
            role = guild.get_role(ID_OF_ROLE_TO_ASSIGN) # right click and "copy id"
            await guild.get_member(message.author.id).add_roles(role)
    if message.content.startswith('!unactivate') and message.channel == message.author.dm_channel:
        guild = client.get_guild(ID_OF_SERVER_TO_TAKE_ROLE_IN)
        if ('PREMIUM MEMBER' in [role.name for role in guild.get_member(message.author.id).roles]):
            await guild.get_member(message.author.id).remove_roles(guild.get_role(ID_OF_ROLE_GOES_HERE))
        else:
            await message.channel.send('You did not verify yet')

A drawback is that this does not support multiple servers, only 1, but you can make different activator commands with different server IDs.

Get the ID of a guild by right clicking the icon/banner and choosing "copy id". Get the ID of a role by right clicking a role (either in server settings or in someones profile) and choosing "copy id".

1
  • i did not test your answer but i made it work now. Thank you anyways for your answer
    – Tim
    Jun 2, 2020 at 19:21
0

You'll need the guild object that the bot is in, so then it knows where to get the role from. Additionally, the message.author object will return a User, not a Member, but we'll need the Member object in order to add a role.

@client.event
async def on_message(message):
    if message.content.lower().startswith("!activate") and not message.guild:
        guild = client.get_guild(112233445566778899) # the guild's ID
        role = discord.utils.get(guild.roles, name="PREMIUM MEMBER") # or you can use id=
        member = await guild.fetch_member(message.author.id)
        await member.add_roles(role)

References:

1
  • No worries! Good luck with the bot :^)
    – Diggy.
    Jun 2, 2020 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.