Discord.py Ban Command Issues - python

Im having an issue with my ban command, my admins can ban each otherr and i dont want that,but im not sure how to fix it here my code
#Ban command
#client.command()
#commands.has_permissions(ban_members=True)
async def ban(ctx, member : discord.Member, *, reason=None):
await member.ban(reason=reason)
await ctx.send(f'{user.mention} has been banned!')
I want it to make it like this but im pretty new to python and idk how to write it (the commented section)
#Ban command
#client.command()
#commands.has_permissions(ban_members=True)
async def ban(ctx, member : discord.Member, *, reason=None):
#if mentioned user has the same role as the author:
await ctx.send('Cant ban Moderators/Admins')
else:
await member.ban(reason=reason)
await ctx.send(f'{user.mention} has been banned!')

#Ban command
#client.command()
#commands.has_permissions(ban_members=True)
async def ban(ctx, member : discord.Member, *, reason=None):
check = False
for i in member.roles:
if i in ctx.author.roles[1:]:
check = True
if(check):
await ctx.send('Cant ban Moderators/Admins')
else:
await member.ban(reason=reason)
await ctx.send(f'{user.mention} has been banned!')

Related

Discord.py: How would you restrict a certain command to a role or people with specific permissions?

Could someone show how you'd restrict, for example, the following command to lets say a role named 'Moderator' or only to people that have kick permissions? I don't quite understand how that'd work.
#client.command()
async def kick(ctx, member:discord.Member)
await member.kick()
await ctx.send(f'{member.mention} has been kicked.')
To restrict commands to certain roles named "Moderator" or "Admin" use has_any_role
#client.command(pass_context=True)
#commands.has_any_role("Moderator", "Admin")
async def kick(ctx, member: discord.Member, *, reason=None):
await member.kick(reason=reason)
await ctx.channel.send(f'{member.mention} has been kicked.')
To restrict commands to certain permissions of roles for example if a role has administrator you use has_permissions
#client.command(pass_context=True)
#commands.has_permissions(administrator=True)
async def kick(ctx, member: discord.Member, *, reason=None):
await member.kick(reason=reason)
await ctx.channel.send(f'{member.mention} has been kicked.')

Programming a Discord bot in Python- How do I make a kick command?

I want to make a command that kicks a specified user when prompted. Here's what I have:
#bot.command()
async def kick(ctx, user: discord.Member, *, reason="No reason provided"):
await user.kick(reason=reason)
kick = discord.Embed(title=f"Kicked {user.name}!", description=f"Reason: {reason}\nBy: {ctx.author.mention}")
await ctx.message.delete()
await ctx.channel.send(embed=kick)
await user.send(embed=kick)
It doesn't seem to be working, any tips?
Here are two things. Since your on_message events are working using client.event, this means that you should replace your bot.command with client.command as seen below:
#client.command()
async def kick(ctx, user: discord.Member, *, reason="No reason provided"):
await user.kick(reason=reason)
kick = discord.Embed(title=f"Kicked {user.name}!", description=f"Reason: {reason}\nBy: {ctx.author.mention}")
await ctx.message.delete()
await ctx.channel.send(embed=kick)
await user.send(embed=kick)
If your kick command still doesn't work, at the very end of your on_message event, you should add await client.process_commands(message). I'll put an example of this below below:
#client.event
async def on_message(message):
if message.content == "Test":
print("recieved")
await client.process_commands(message)

"mute" command not working at all (Discord.py, rewrite)

So I am currently making a bot for my Discord server and I wanted to add a 'mute' command, but it didnt work at all here's the code(I am using python 3.8.6)
#client.command()
#commands.has_any_role('Trial Moderator', 'Moderator', 'Administrator', 'Owner')
async def mute(ctx, member : discord.Member, *, reason=None):
muted_role = get(Guild.roles, id='760885845233631233')
for role in member.roles:
if role == muted_role:
await ctx.send(f'{member} is already muted!')
break
member.add_roles(muted_role)
await ctx.send(f'Muted {member}\nReason: {reason}')
There are no errors or anything when I run the command, and I am 100% sure that I have sufficient permissions, and when I run any other command they work perfectly
Your mistake is in this line:
muted_role = get(Guild.roles, id='760885845233631233')
Your ID must be an interger, not a string. You also provided a blank Guild object so it can find any role:
muted_role = get(ctx.guild.roles, id=760885845233631233)
You can also do a bit of refactoring:
#client.command()
#commands.has_any_role('Trial Moderator', 'Moderator', 'Administrator', 'Owner')
async def mute(ctx, member : discord.Member, *, reason=None):
muted_role = get(ctx.guild.roles, id=760885845233631233)
if muted_role in member.roles:
await ctx.send(f'{member} is already muted!')
else:
member.add_roles(muted_role)
await ctx.send(f'Muted {member}\nReason: {reason}')

Discord.py rewrite : "Mute" command not working

I am attempting to mute a user.
#client.command()
#commands.has_permissions(administrator=True)
async def mute(ctx, user: discord.Member, *, reason=None):
await user.mute(reason=reason)
await ctx.send(f'{user} has been muted')
I receive the following error:
Member object has no attribute "mute"
How can I mute a user?
Try this:
#client.command()
#commands.has_permissions(administrator=True)
async def mute(ctx, user : discord.Member, *, reason = None):
await user.edit(mute = True, reason = reason)
await ctx.send(f'{user} has been muted')

discord.py Rewrite - How to Check if a discord.Member is a bot

I need to check if a discord.Member is a bot in this command:
async def kick(ctx, user: discord.Member, *, reason="No Reason Provided"):
Thanks in advance!
#bot.command()
async def kick(ctx, user: discord.Member, *, reason="No reason provided"):
if not user.bot:
await user.kick(reason=reason)
await ctx.send(f"Successfully kicked {user}!")
else:
await ctx.send("You can't kick a bot!")
References:
Member.bot
Member.kick()

Categories