How to make commands parameters case insensitive? - python

So basically I want to make so the commands parameters will be case insensitive. For example: ?role [member] [role]. So, I don't have to type the full name or the same capitalization of the member and role name. Is it really possible? Because I've tried Dyno bot, and it seems like it's possible. I've tried this code but it doesn't work:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix=("d", "D"), intents=intents, help_command=None, case_insensitive=True)
Like this, I didn't type the member name fully and the role name with the same capitalization.

If it's only for members (like in the image), you can specify the parameter to be a member object, then you can give the name, mention the member or give the id of the member.
An example would be:
#commands.command(name="userinfo")
async def userinfo(self, ctx, Member : discord.Member):
# Do your stuff

Lower the parameters using .lower()
#commands.command()
async def role(self, ctx, Member : discord.Member, Role):
role = Role.lower()
If you want the first letter capitalized then do this:
#commands.command()
async def role(self, ctx, Member : discord.Member, Role):
role = Role.capitalize()

You can simply use discord's converter's
#commands.command()
async def role(self, ctx, member : discord.Member = None, Role: discord.role = None):
if role is None or member is None:
await ctx.send('you need to specify a member and a role')
else:
if role in member.roles():
await member.remove_role(role)
else:
await member.add_role(role)
References:
Converters
Role converter
Member converter
roles

Related

Discord.py adding roles not picking up more than 1 word role

#client.command()
#commands.has_role("mod")
async def addrole(ctx, member : discord.Member, role : discord.Role):
await member.add_roles(role)
#client.command()
#commands.has_role("mod")
async def removerole(ctx, member : discord.Member, role : discord.Role):
await member.remove_roles(role)
This code works perfectly fine with roles with one word but once I try to use it with a role with 2 words it doesn't pick up the second word. For example if I asked it to add a role called "dark blue" then this pops up
discord.ext.commands.errors.RoleNotFound: Role "dark" not found.
is there a fix to this or do I have to redo roles in my discord server?
There are two things that are going on.
If the command parameter role should be a string, you will need to use discord.utils.get() to get a discord.Role object.
#client.command()
#commands.has_role("mod")
async def addrole(ctx, member : discord.Member, role : str):
roleobj = discord.utils.get(ctx.guild.roles, name=role)
if roleobj is not None:
await member.add_roles(roleobj)
#client.command()
#commands.has_role("mod")
async def removerole(ctx, member : discord.Member, role : str):
roleobj = discord.utils.get(ctx.guild.roles, name=role)
if roleobj is not None:
await member.remove_roles(role)
If the command parameter role should be a discord.Role object, members will have to mention the role when invoking the command.
!addrole #MemberName #RoleName or !removerole #MemberName #RoleName
If (1) is the intended usage
If you check out the documentation for the discord.ext.commands framework, you'll see a note from the developers on passing positional arguments which contain spaces:
To make use of a word with spaces in between, you should quote it
So, everything which you have currently set up should work perfectly fine. It would just be a matter of telling your guild members that role names that contain spaces should be quoted. For example:
!addrole #MemberName "dark blue"
or
!removerole #MemberName "dark blue"
If (2) is the intended usage
This is just be a matter of telling your guild members that roles should be mentioned when using the commands.
Discord.py parses arguments by spaces. For example:
#commands.command()
async def say(self, ctx, to_say: str):
await ctx.send(to_say)
# !say hello world
# hello
This can be solved in two ways:
The first is to change the command to accept more than one word:
#commands.command()
async def say(self, ctx, *, to_say: str):
await ctx.send(to_say)
# !say hello world
# hello world
You can also put arguments inside of quotes, and discord.py won't split them.
#commands.command()
async def say(self, ctx, to_say: str):
await ctx.send(to_say)
# !say "hello world"
# hello world
So in your case:
#client.command()
#commands.has_role("mod")
async def addrole(ctx, member : discord.Member, *, role : discord.Role):
await member.add_roles(role)
#client.command()
#commands.has_role("mod")
async def removerole(ctx, member : discord.Member, *, role : discord.Role):
await member.remove_roles(role)
# This will now accept dark blue as a role, instead of only taking dark
Also, a note on the other answer. It is not true that you have to mention the role, commands.RoleConverter will accept names, ids, or mentions. There is no reason to use anything except discord.Role for converters in commands.

How could i get the name of a role in discord.py

I created an add role command. It works fine but my problem is that it works only with the id of the role. I was wondering how could I somehow convert the id of the role to the name of the role.
This is the code.
#commands.has_permissions(manage_messages=True)
async def addrole(self, ctx, member: discord.Member,rolename):
if member is None:
member=ctx.author
role = discord.utils.get(ctx.guild.roles, id=rolename)
await member.add_roles(role)
await ctx.send(f"{member.mention}was added {rolename}")
Any help is appreciated.edit
solution
I made use of the 2 comments and created this command which works perfectly.
#commands.command()
#commands.has_permissions(manage_messages=True)
async def addrole(self, ctx, role: discord.Role, member: discord.Member=None):
if member is None:
member=ctx.author
await member.add_roles(role)
await ctx.send(f"{member.mention} was added {role}")```
You could declare your rolename variable as a discord.Role and make use of the implicit role converter that will be applied, just like what happens to your member object. Your function would look something like this:
#commands.has_permissions(manage_messages=True)
async def addrole(self, ctx, member: discord.Member, role: discord.Role):
await member.add_roles(role)
await ctx.send(f"{member.mention} was added {rolename}")
You can use role converter just like you used member converter. You can pass role: discord.Role in arguments. Then, you just have to mention the role.
Also, you have to assign a default value None to the member argument in order to check if it's None.
#commands.has_permissions(manage_messages=True)
async def addrole(self, ctx, role: discord.Role, member: discord.Member=None):
if not member:
member=ctx.author
await member.add_roles(role)
await ctx.send(f"{member.mention}was added {role.name}")
References
RoleConverter
discord.Role.name

How to add a role with a Discord Bot

I start creating a Discord bot with the Python API of Discord. So, I tried to make a command that give roles on a discord server. Here is my code :
client = commands.Bot(command_prefix = '/')
#client.command()
async def addrole(ctx, role: discord.Role, user: discord.Member):
if ctx.author.guild_permissions.administrator:
await user.add_roles(role)
await ctx.send(f"Successfully given {role.mention} to {user.mention}.")
When I try to use the command, any error happear. I try to make like the websearch I've done but still don't work. Can someone help me ?
I don't believe basic things like these would require Discords intents, especially the "member intent". What I have done is I've cleaned up your code and required a member to be passed before a multiple string role, if needed.
#client.command()
#commands.has_permissions(manage_roles=True)
async def addrole(ctx, member: discord.Member = None, *, role: discord.Role = None):
if role == None:
await ctx.send(f'Provide a role to add')
return
if member == None:
await ctx.send(f'Provide a member to add a role')
return
await member.add_roles(role)
await ctx.send(f"Successfully added role, {role} to {member.name}")

(Discord.py) My command that gives roles to a user is not working

I'm trying to code it so that someone can give another person a role using a .role <#member> command, so it'll look something like this:
.role #Daily Cringe
Ideally, the bot will give Daily the "Cringe" role. However, it is not working.
Here's the code:
#client.command()
async def role(ctx, member: discord.Member = ctx.author, role: discord.Role):
await ctx.member.add_roles(role)
await ctx.send(f"The role ''**{role}**'' was just given to you")
I tried to set the default value of member to ctx.author but then I get an issue:
async def role(ctx, member: discord.Member = ctx.author, role: discord.Role):
^
SyntaxError: non-default argument follows default argument
I tried to change it so that the role variable's default value is equal to None, but that doesn't seem to work either.
Any ideas?
I use Visual Studio Code, Python 3.8.6 64-bit and MacOS Catalina 10.15.7
Your code has several mistakes. At first, you can't do member: discord.Member = ctx.author because ctx is not defined yet. You have to set member's default value to None and check in the command if the member is None.
Then you can't add role with ctx.member.add_roles(role). You have to do member.add_roles(role).
#client.command()
async def role(ctx, member: discord.Member = None, role: discord.Role):
if not member:
member = ctx.author
await member.add_roles(role)
await ctx.send(f"The role ''**{role}**'' was just given to you")

Using discord.py, how can I add a role to a user that contains spaces in its name?

I have seen that you can add a role using a bot using discord.py. I want to make my code so that you can add roles with multiple words as its name so you don't get errors like 'Role "Chief" not found' when the command was -giverole Chief Executive Officer. I also want to add another parameter in which you ping the user you want to give the role to.
This is for my discord server. I have got a working code that can give a role to you (whoever executes the command) and the role can only be one word: E.g. 'Members'
#client.command(pass_context=True)
async def giverole(ctx, role: discord.Role):
await client.add_roles(ctx.message.author, role)
Input: -giverole <mention_user> <role_name> (must be compatible to give a role that is multiple words)
Output: I can sort out a message that it sends.
You can use a converter to get the Member to send the role to just like you use it to get the Role itself. To accept multiple-word roles, use the keyword-only argument syntax. You can also use role mentions when invoking the command. (e.g. !giverole #Patrick #Chief Executive Officer).
#client.command(pass_context=True)
async def giverole(ctx, member: discord.Member, *, role: discord.Role):
await client.say(f"Giving the role {role.mention} to {member.mention}")
await client.add_roles(member, role)
You can use discord.utils.get to get the role from a string, in this case role_name
#client.command(pass_context=True)
async def giverole(ctx, *,role_name:str):
role = discord.utils.get(ctx.message.server.roles, name=role_name)
await client.add_roles(ctx.message.author, role)

Categories

Resources