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} ')
Related
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.
So, I have created a discord.py bot, and I am adding a functionality to the bot so it can ban and kick people. There is a problem, however. Whenever I enter !!kick #user_account OR !!ban #user_account, it does nothing. I have also tried changing the work "ctx" to "message", but the same thing happens. Help would be very much appreciated, thank you.
#client.command()
async def kick(ctx, member: discord.Member, *, reason=None):
await member.kick(reason=reason)
#client.command()
async def ban(ctx, member: discord.Member, *, reason=None):
await member.ban(reason=reason)
here is ban kick unban code discord py and the error under the command is so people with no perms cant do them
#--- kick command --- #client.command() #commands.guild_only() #commands.has_permissions(kick_members=True) async def kick(ctx, user:
discord.Member, *, reason=None): await user.kick(reason=reason)
await ctx.send(f"{user} Has been kicked!")
#kick.error async def kick(ctx, error): if isinstance(error,
commands.MissingPermissions):
await ctx.send("You dont have premissions to run this command")
#--- ban command --- #client.command() #commands.guild_only() #commands.has_permissions(ban_members=True) async def ban(ctx, user:
discord.Member, *, reason=None):
await user.ban(reason=reason)
await ctx.send(f"{user} Has been banned!")
#ban.error async def ban(ctx, error): if isinstance(error,
commands.MissingPermissions):
await ctx.send("You dont have premissions to run this command")
#--- unban command --- #client.command() #commands.guild_only() #commands.has_permissions(ban_members=True) async def unban(ctx, id:
int):
user_id = await client.fetch_user(id)
await ctx.guild.unban(user_id)
await ctx.send(f"{user_id} Has been unbanned!")
#unban.error async def ban(ctx, error): if isinstance(error,
commands.MissingPermissions):
await ctx.send("You dont have premissions to run this command")
I have my own bot and it appears to work on my end. Since you say no error is printed, you'll have to print out what the error is by doing something like the following:
#client.event
async def on_command_error(ctx, error):
if isinstance(error, commands.CommandError): # if a command fails
print(error) # print the error
OR, if you want to do it for each individual command:
#kick.error
async def kick_error(ctx, error):
if isinstance(error, commands.CommandError): # if a command fails
print(error) # print the error
#ban.error
async def ban_error(ctx, error):
if isinstance(error, commands.CommandError): # if a command fails
print(error) # print the error
Once you find out the error, try solving it.
I need an efficent discord.py command for unban users by their tag, not their name and discriminator. how i can do?
This is the code that I made and it works with *unban name#1234.
#client.command()
#commands.has_any_role(702909770956406885, 545323952428417064, 545323570587369472)
async def unban(ctx, *, member):
banned_user = await ctx.guild.bans()
member_name, member_discriminator = member.split("#")
for ban_entry in banned_user:
user = ban_entry.user
if (user.name, user.discriminator) == (member_name, member_discriminator):
await ctx.guild.unban(user)
embed = discord.Embed(title="Fatto!", description=f"Ho sbannato {user.mention}!", color=discord.Color.green())
await ctx.send(embed=embed)
return
How can i make it work with tags? I know you can't directly tag a banned user but with his id you can do it. Thank you for answers!
Hope this helps
from discord.ext.commands import has_permissions
#bot.command()
#has_permissions(ban_members=True)
async def unban(ctx, userId: discord.User.id):
user = get(id=userId)
await ctx.guild.unban(user)
Use the member object as a parameter in your unban function.
Ex:
#client.command()
#commands.has_any_role(702909770956406885, 545323952428417064, 545323570587369472)
async def unban(ctx, *, member: discord.Member):
await ctx.guild.unban(member)
Then in discord you could execute the command by mentioning the user.
Ex: *unban #aleee
#client.command(description="bans a user with specific reason (only admins)") #ban
#commands.has_permissions(administrator=True)
async def ban (ctx, member:discord.User=None, reason =None):
try:
if (reason == None):
await ctx.channel.send("You have to specify a reason!")
return
if (member == ctx.message.author or member == None):
await ctx.send("""You cannot ban yourself!""")
else:
message = f"You have been banned from {ctx.guild.name} for {reason}"
await member.send(message)
await ctx.guild.ban(member, reason=reason)
print(member)
print(reason)
await ctx.channel.send(f"{member} is banned!")
except:
await ctx.send(f"Error banning user {member} (cannot ban owner or bot)")
I'm currently trying to make a discord bot with the ability to .mute a user. I have created this script so far that allows people with a "staff" role to run the command, and gives the tagged user a "Muted" role. It also creates one if it doesn't exist already. The problem is the code below does not work. It doesn't say anything in the console but if you have the staff role and you run the command nothing happens.
#commands.has_role("staff")
async def mute(ctx, member: discord.Member=None):
guild = ctx.guild
if (not guild.has_role(name="Muted")):
perms = discord.Permissions(send_messages=False, speak=False)
await guild.create_role(name="Muted", permissions=perms)
role = discord.utils.get(ctx.guild.roles, name="Muted")
await member.add_roles(role)
print("🔨 "+member+" was muted.")
if (not member):
await ctx.send("Please specify a member to mute")
return
#mute.error
async def mute_error(ctx, error):
if isinstance(error, commands.CheckFailure):
await ctx.send("You don't have the 'staff' role")
This specific line:
print("🔨 "+member+" was muted.")
Prints it in the terminal, or wherever you ran the command. Try await ctx.send Also, try using f-strings if you have version > 3.6 of Python. Also, your error is wrong.
#client.command()
#commands.has_role("staff")
async def mute(ctx, member: discord.Member):
role = discord.utils.get(ctx.guild.roles, name="Muted")
guild = ctx.guild
if role not in guild.roles:
perms = discord.Permissions(send_messages=False, speak=False)
await guild.create_role(name="Muted", permissions=perms)
await member.add_roles(role)
await ctx.send(f"🔨{member} was muted.")
else:
await member.add_roles(role)
await ctx.send(f"🔨{member} was muted.")
#mute.error
async def mute_error(ctx, error):
if isinstance(error, commands.MissingRole):
await ctx.send("You don't have the 'staff' role")
#mute.error
async def mute_error(ctx, error):
if isinstance(error, commands.BadArgument):
await ctx.send("That is not a valid member")
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)