Discord.py bot not responding to mute command - python

Im trying to make a mute command in discord.py and everything seems to be correct. Whenever i use the command then my bot doesn't respond with the message i've gave it, and it doesn't give the muted role. Here's the code:
#commands.has_permissions(manage_roles=True)
async def mute(ctx, user: discord.Member, *, reason="No reason provided"):
await user.mute(reason=reason)
role = discord.utils.get(ctx.guild.roles, name="Muted")
mute = discord.Embed(title=f"User {user.name}#{user.discriminator} has been muted. <a:m_verifyblack:850825891780100096>", color=0xF4D03F, description=f"Reason: {reason}\nBy: {ctx.author.mention}")
await ctx.message.delete()
await ctx.channel.send(embed=mute)
await user.send(embed=mute)
#mute.error
async def mute_error(ctx, error):
if isinstance(error, commands.MissingPermissions):
await ctx.send("**:no_entry_sign: You cant do that!**")```

You appear to be missing the #client.command (if you did client = commands.Bot(command_prefix="prefix") at the beginning of the file under imports) at the beginning of the command. To fix this you would do
import discord
from discord.ext import commands
from discord.ext.commands import MissingPermissions
client = commands.Bot(command_prefix="your_prefix")
#client.command(aliases=['m'])
#commands.has_permissions(manage_roles=True)
async def mute(ctx, user: discord.Member, *, reason="No reason provided"):
role = discord.utils.get(ctx.guild.roles, name="Muted")
mute = discord.Embed(title=f"User {user.name}#{user.discriminator} has been muted. <a:m_verifyblack:850825891780100096>", color=0xF4D03F, description=f"Reason: {reason}\nBy: {ctx.author.mention}")
await user.mute(reason=reason)
await ctx.message.delete()
await ctx.channel.send(embed=mute)
await user.send(embed=mute)
#mute.error
async def mute_error(ctx, error):
if isinstance(error, commands.MissingPermissions):
await ctx.send("**:no_entry_sign: You cant do that!**")
Also you seem to be using an outdated version of discord.py . Try switching to Pycord, which is a maintained fork of the now-deprecated discord.py. I'm not quite sure what user.mute does, but hopefully I answered your question.

Related

Discord.py on_message cancelling other commands

I am trying to build a simple bot that can kick, ban, and say "Hi" to a member. Kick and ban work perfectly fine and do their job, but when I add an "on_message" function, the on_message function works but cancels out kick and ban. Any ideas?
My code:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix="!")
#bot.command()
async def kick(ctx, member: discord.Member, *, reason=None):
await member.kick(reason=reason)
#bot.command()
async def ban(ctx, member: discord.Member, *, reason=None):
await member.ban(reason=reason)
#bot.event
async def on_message(message):
if "!hello" == message.content:
await message.channel.send(f"Hi")
await bot.process_commands(message)
bot.run("my token")
First, I would make !hello a command:
#bot.command()
async def hello(ctx):
author=ctx.author
await ctx.send(f'Hello, {author.mention}!')
Then change your on_message function to:
#bot.event
async def on_message(msg):
await bot.process_commands(msg)
I usually keep event functions before the commands for organization's sake. Here though, unless you'll have something else to check for when a user sends a message, I don't think you technically need an async def on_message/await bot.process_commands event.
From my personal experience events and commands don't do well when they are in the same file, consider using cogs to seperate them.

discord.py member.edit(nick=nickname) not working

I'm making a Discord bot using discord.ext. I want to change the users nickname on the command nick.
For that I wrote following code:
from discord.ext import commands
import discord
# set prefix as "w!"
bot = commands.Bot(command_prefix="w!")
# do stuff when certain error occurs
#bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.BotMissingPermissions):
await ctx.send("Error: Bot missing permissions.")
if isinstance(error, commands.CommandNotFound):
await ctx.send("Error: The command was not found")
if isinstance(error, commands.MissingPermissions):
await ctx.send("Error: You don't have permission to do that.")
#bot.command()
# check if the user has the permission to change their name
#commands.has_permissions(change_nickname=True)
async def nick(ctx, member: discord.Member, *, nickname):
await member.edit(nick=nickname)
await ctx.send(f"Nickname was changed to {member.mention}.")
# reset the nickname if no new name was given
#nick.error
async def nick_error(ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
member = ctx.author
await member.edit(nick=None)
await ctx.send(f"Reset nickname to {member.mention}.")
bot.run("TOKEN")
When I enter w!nick test name in Discord it doesn't respond to the message and doesn't change my nickname. But when I just enter w!nick it resets my nickname.
Thanks to #Mr_Spaar for helping.
The solution:
#bot.command()
#commands.has_permissions(change_nickname=True)
async def nick(ctx, *, nickname):
member = ctx.author
await member.edit(nick=nickname)
await ctx.send(f"Nickname was changed to {member.mention}.")
Does this help?
client.command(pass_context=True)
#commands.has_permissions(change_nickname=True)
async def hnick(ctx, member: discord.Member, nick):
await member.edit(nick=nick)
await ctx.send(f'Nickname was changed for {member.mention} ')

My discord.py bot cannot check roles in command

#client.command()
async def expelliarmus(ctx, member: discord.Member):
if member.have_roles("protego"):
await ctx.send("Protego yapan birine saldıramazsın.")
else:
duelrole = discord.utils.get(ctx.guild.roles, name="düellocu")
await member.remove_roles(duelrole)
await ctx.send("ASASI UÇTU! blablabla")
await asyncio.sleep(30)
await member.add_roles(duelrole)
await ctx.send("Asasını geri aldı!!")
I'm building a Discord bot and I want the command not to work if the user has the role named Protego. What can I do?
u need to definite exception for unregistered a role 'protego'.

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)

I need help making a discord py temp mute command in discord py

I got my discord bot to have a mute command but you have to unmute the user yourself at a later time, I want to have another command called "tempmute" that mutes a member for a certain number of minutes/hours/ or days, this is my code so far, how would I make a temp mute command out of this?
#mute command
#client.command()
#commands.has_permissions(kick_members=True)
async def mute(ctx, member: discord.Member=None):
if not member:
await ctx.send("Who do you want me to mute?")
return
role = discord.utils.get(ctx.guild.roles, name="muted")
await member.add_roles(role)
await ctx.send("ok I did it")
Similar to how you gave them a role to mute them, just add another parameter to take in how long you want them to be muted in seconds. Then you can use await asyncio.sleep(mute_time) before removing that role.
The code will look something like:
import asyncio
#mute command
#client.command()
#commands.has_permissions(kick_members=True)
async def mute(ctx, member: discord.Member=None, mute_time : int):
if not member:
await ctx.send("Who do you want me to mute?")
return
role = discord.utils.get(ctx.guild.roles, name="muted")
await member.add_roles(role)
await ctx.send("ok I did it")
await asyncio.sleep(mute_time)
await member.remove_roles(role)
await ctx.send("ok times up")
import asyncio
#commands.command()
#commands.has_permissions(manage_messages=True)
async def mute(ctx, member: discord.Member,time):
muted_role=discord.utils.get(ctx.guild.roles, name="Muted")
time_convert = {"s":1, "m":60, "h":3600,"d":86400}
tempmute= int(time[0]) * time_convert[time[-1]]
await ctx.message.delete()
await member.add_roles(muted_role)
embed = discord.Embed(description= f"✅ **{member.display_name}#{member.discriminator} muted successfuly**", color=discord.Color.green())
await ctx.send(embed=embed, delete_after=5)
await asyncio.sleep(tempmute)
await member.remove_roles(muted_role)

Categories