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.
Related
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} ')
So I want to purge the message of a particular member. This is my code.
#commands.command()
async def purge_member(self, ctx, member: discord.Member):
await ctx.channel.purge(limit=100, check=lambda message: message.author == member)
The problem is the messages of the member are not getting purged.
It does not show any error either.
I have tried many other methods instead of purge to delete but all of them refuse to work, with no errors
If you have an on_message event, add process_commands at the end of it
# In Cog
#commands.Cog.listener()
async def on_message(m):
...
await self.bot.process_commands(m)
# In Main File
#bot.event
async def on_message(m):
...
await bot.process_commands(m)
If you have not un-commented the code do it else it won't work
# In Cog
#commands.command()
async def purge_member(self, ctx, member: discord.Member):
await ctx.channel.purge(limit=100, check=lambda message: message.author == member)
# In Main File
#bot.command()
async def purge_member(ctx, member: discord.Member):
await ctx.channel.purge(limit=100, check=lambda message: message.author == member)
#bot.command()
async def purge(ctx, limit=100, member: discord.Member=None):
await ctx.message.delete()
msg = []
for m in ctx.channel.history():
if m.author == member:
msg.append(m)
await ctx.channel.delete_messages(msg)
heres my code, this is a purge command with discord.py. Im trying to make it so it purges the message of someone you mention. Any help?
The reason this isn't working is because ctx.channel.history() is an async function, so you'll have to use the async keyword before the loop. Like this:
#bot.command()
async def purge(ctx, limit=100, member: discord.Member=None):
await ctx.message.delete()
msg = []
async for m in ctx.channel.history():
if m.author == member:
msg.append(m)
await ctx.channel.delete_messages(msg)
You can also just use the purge function with a lambda instead. If you were to go that route, your code would look like this:
#bot.command()
async def purge(ctx, limit=100, member: discord.Member=None):
await ctx.message.delete()
deleted = await ctx.channel.purge(limit=100, check=lambda msg: msg.author == member)
await ctx.channel.send('Deleted {} message(s)'.format(len(deleted)))
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)
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!')