Discord.py how do I make a mute command? - python

As the title says. I'm trying to figure out how to make a mute command using discord.py rewrite. I'm thinking that we need to have a "mute" role where the command used gives the user the "mute" role and for how long. How do I achieve this.
I already have
#bot.command()
#commands.has_permissions(mute_members)
async def mute(ctx, member:discord.Member):

The best way to do this would be to have a database setup in which you can add and remove users. Then you can use the on_message event and check if the author is in the database, if it is then delete the message. You could also just use a list/file for it to be simpler.

you can create a Muted role and make your bot add the role to the user you want to mute:
#bot.command()
async def mute(ctx, member: discord.Member):
role = discord.utils.get(ctx.guild.roles, name='Muted')
await member.add_roles(role)
await ctx.send("role added")

Related

Removing every role from a user discord.py

I wanna do a ban appeal system on my server. I wanna remove every single role of someone and give them the [Banned] role. I cannot just do something like I did for the member role for all of them since there are a lot of roles, even some custom ones that are made and deleted every day.
member_role = get(user.guild.roles, name="γ€Žβœ…γ€Β· π™ΌπšŽπš–πš‹πš›πšž")
await user.remove_roles(member_role, reason=None, atomic=True)
I tried this: discord.py trying to remove all roles from a user but it didn't work. Also tried this:
for role in user.roles:
if role.name == '[Banned]':
pass
else:
await user.remove_roles(role)
but couldn't get it to work. (I have no experience in python or discord.py)
So. How can I remove every role from a user instead of only the member_role ?
#bot.command()
#commands.has_permissions(ban_members=True)
async def ban(ctx, user: discord.Member, *, reason=None):
await asyncio.sleep(1)
banned_role = get(user.guild.roles, name="[Banned]")
await user.add_roles(banned_role, reason=None, atomic=True)
member_role = get(user.guild.roles, name="γ€Žβœ…γ€Β· π™ΌπšŽπš–πš‹πš›πšž")
await user.remove_roles(member_role, reason=None, atomic=True)
banemb = discord.Embed(title="Ban", description=f"{user.mention} a fost banat/a. ", colour=discord.Colour.dark_red())
banemb.add_field(name="Motiv:", value=reason, inline=False)
await ctx.send(embed=banemb)
You can't get the list of roles that way.
To remove all the roles of the user you have to apply an edit to it and provide an empty list of roles.
It also changes the order of your code as you first have to remove and then re-assign a role to the user.
Have a look at the following code:
#bot.command()
#commands.has_permissions(ban_members=True)
async def ban(ctx, user: discord.Member, *, reason=None):
await asyncio.sleep(1)
banned_role = discord.utils.get(user.guild.roles, name="[Banned]")
await user.edit(roles=[]) # Remove all roles
await user.add_roles(banned_role, reason=None) # Assign the new role
banemb = discord.Embed(title="Ban", description=f"{user.mention} a fost banat/a. ",
colour=discord.Colour.dark_red())
banemb.add_field(name="Motiv:", value=reason, inline=False)
await ctx.send(embed=banemb)
I have changed/removed a few things from the code. Of course you have to add them again, according to your wishes.

Discord.py: How to change role color?

So i created this role for my discord bot and i want to change his color to yellow.
I don't know why it won't change the color. This is my code, can anyone help me with it?
#bot.command(pass_context=True)
async def add_role(ctx):
member = ctx.author
role = discord.utils.get(member.guild.roles, name="Spike")
await member.add_roles(role)
await role.edit(colour=discord.colour(0xFFFF00))
print("done")
edit: It doesn't print the "done"
On the following line await role.edit(colour=discord.colour(0xFFFF00)), it looks like you missed the capitalisation of Colour in the discord class. This should be correct, unless the API already handles this.
await role.edit(colour=discord.Colour(0xFFFF00))
Can you try this:
#bot.command(pass_context=True)
async def add_role(ctx):
member = ctx.author
role = discord.utils.get(member.guild.roles, name="Spike")
await member.add_roles(role)
await role.edit(colour=0xFFFF00)
print("done")
Ok so, my problem was that my bot.command wasn't working because I was using bot and client method at the same time. I wasn't aware that they can't work togheter, so I decided to only use bot, and now everything is working.

Discord.py Adding Reactions For Poll

I've got this code for my bot, I'm trying to make a !poll command. In theory, I'll type !poll (question), and then the bot will send my questions as an embed, and under that embed, add a thumbsup and thumbsdown reaction so people can vote. However, I just cannot work out how to make it add the reaction, no matter how hard I try and research. Here's the code:
#commands.has_permissions(administrator=True)
async def poll (ctx, *, message):
embed=discord.Embed(title="New Poll!", description = message, color=0xff0000)
await ctx.send(embed=embed)
So what do I now need to add to my code so it can add a πŸ‘ and πŸ‘Ž reaction to the embed? Thanks.
Messageable.send returns a discord.Message instance, you then simply .add_reaction()
message = await ctx.send(embed=embed)
await message.add_reaction('πŸ‘')
await message.add_reaction('πŸ‘Ž')
Reference:
Messageable.send
Message.add_reaction

Multiple roles being allowed for one command with discord.py

How would I go about making it so that an admin or moderator could clear a Discord chat?
#commands.has_role("Admin")
async def addrole(ctx, rol='member'):
member = ctx.message.author
role = get(member.server.roles, name=rol)
await bot.add_roles(member, role)
You can use the commands.has_any_role check instead.
I prefer:
if role_id(admin etc.) in [role.id for role in ctx.messsage.author.roles]:

discord.py | Making a mute command, can't get member name

I am setting up a mute command for my new discord bot, I am fairly new to the discord.py stuff, and do not understand what is going wrong.
I keep getting the error that a member is not being specified, when it clearly is.
I have tried many tutorials on youtube etc, but it always skims over a detail or two so I can't fully figure it out. I would appreciate it if someone could correct my code, because I am still learning discord.py.
#client.command()
async def mute(context, member: discord.Member=None):
if not member:
await client.say('Please specify a member')
return
role = get(member.server.roles, name="Muted")
await client.add_roles(member, role)
await client.say('{member.mention} was muted.')
It is just supposed to add the muted role to someone, and be done with it. I am having the same issue with specifying a member when using my ban and kick commands too, which are done in the same fashion.
I am open to all suggestions, thank you!
You need to change the decorator to #client.command(pass_context=True). The member name is being assigned to context, leaving member to take the default value.
#client.command(pass_context=True)
async def mute(context, member: discord.Member=None):
if not member:
await client.say('Please specify a member')
return
role = get(member.server.roles, name="Muted")
await client.add_roles(member, role)
await client.say(f'{member.mention} was muted.') # You forgot the f
Also, I would probably just let the conversion fail and then handle the error:
#client.command(pass_context=True)
async def mute(ctx, member: discord.Member):
role = get(member.server.roles, name="Muted")
await client.add_roles(member, role)
await client.say(f'{member.mention} was muted.')
#mute.error:
async def mute_error(error, ctx):
if isinstance(error, ConversionError):
await client.send_message(ctx.message.channel, 'Please specify a member')
else:
raise error

Categories