Home > Blockchain >  How to accept role names that contain spaces?
How to accept role names that contain spaces?

Time:10-27

@client.command()
async def give(ctx, member: discord.Member, role: discord.Role):
  member = member or ctx.message.author
  
  await member.add_roles(role)

This code can give a user a role but it doesn't work if the role has a space in it. How can I accept role names that contain spaces?

CodePudding user response:

You can simply add a * before the role parameter (Python's keyword-only argument specification). This tells discord.py to consume the rest of the command and feed it to the roles converter.

Also, it seems like you want to make the member parameter optional (judging from the first line in the function), this is also possible by using typing.Optional.

import typing

@client.command() 
async def give(ctx, member: typing.Optional[discord.Member] = None, *, role: discord.Role):   
    member = member or ctx.message.author
    await member.add_roles(role)
  • Related