(No errors btw) Why isnt my purge command working? - python

#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)))

Related

kick/ban people in discord.py

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.

discord.py purge command not working and no error

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)

Use a webhook to send a message as someone else

I've been looking into this issue, but only found webhooks that copy the author's name and avatar. I'd like to do the same, but with a tagged member's stuff.
trigger:
--say #test test
result:
#test test
This is my code so far:
#bot.command(pass_context=True)
async def say(ctx, *, message: str, member: discord.Member = None):
async with ClientSession() as session:
webhook = discord.Webhook.from_url(WEBHOOK_URL, adapter=discord.AsyncWebhookAdapter(session))
await webhook.send(content=message, username=member.Member.name, avatar_url=member.avatar_url)
Here is a working example of "impersonating" a user through a webhook, it takes the user's name and their profile picture but simply appears as a bot. It's the same method as bots such as "NQN" uses.
#client.command()
async def say(ctx, member: discord.Member, *, message=None):
if message == None:
await ctx.send(
f'Please provide a message with that!')
return
webhook = await ctx.channel.create_webhook(name=member.name)
await webhook.send(
str(message), username=member.name, avatar_url=member.avatar_url)
webhooks = await ctx.channel.webhooks()
for webhook in webhooks:
await webhook.delete()
Try this
#client.command()
async def impersonate(ctx, member: discord.Member, *, message=None):
if message == None:
await ctx.send(f'Who do you want to impersonate?')
return
webhook = await ctx.channel.create_webhook(name=member.name)
await webhook.send(
str(message), username=member.name, avatar_url=member.avatar_url)
await webhook.delete()

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)

Discord.py Ban Command Issues

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!')

Categories