Pretty much, for some reason the actual purging works, but it wont send the response embed after its purged. Sometimes it does, sometimes it just spams terminal with errors.
Heres is my code:
#tree.command(name= "purge", description="Purge messages in a channel", guild=discord.Object(id=guildId))
#app_commands.checks.has_permissions(manage_messages=True)
async def purge(interaction, limit: int):
await interaction.channel.purge(limit=limit)
embed = discord.Embed(description=f"Purged {limit} message(s)", color=0x3375FF)
await interaction.response.send_message(embed=embed)
Honestly, im not too sure what is wrong with it. I tried it without an embed and that still didnt work.
https://pastebin.com/uibRCN1F Here is a link to the error I am getting in the termina
It might be that your interaction is timing out. Perhaps try deferring and then using the followup property to send your message later.
#tree.command(name= "purge", description="Purge messages in a channel", guild=discord.Object(id=guildId))
#app_commands.checks.has_permissions(manage_messages=True)
async def purge(interaction, limit: int):
await interaction.response.defer()
await interaction.channel.purge(limit=limit)
embed = discord.Embed(description=f"Purged {limit} message(s)", color=0x3375FF)
await interaction.channel.send(embed=embed)
Related
Ok so I've tried everything for my command but nothing worked for my !say command to work and not tag #everyone. I tried doing it myself but it still doesn't want to. I'm a beginner so i'm bad but where is the problem and what do i do?
here is my code:
async def say(ctx, msg=None):
if msg is not None:
await ctx.send(msg)
await ctx.message.delete()
if message == "#everyone" or "#here":
break:
Avoid specific mention when sending a message
discord.py already has a feature built-in, to allow or not specific type of mention.
Combining discord.AllowedMentions and discord.abc.Messageable.send, as the following example.
async def avoid_everyone(ctx: commands.Context, *message: str)
# Disallow everyone when sending message
allowed = discord.AllowedMentions(everyone=False)
# Raise an exception if #everyone in message
await ctx.send(message, allowed_mentions=allowed)
I just recently started using cogs/extension files for Discord.py and ran into some issue that I don't know how to fix. I'm trying to send a message in a specific channel but I always just get the error AttributeError: 'NoneType' object has no attribute 'send'. I know that I can send a message in a specific channel with:
#client.command()
async def test(ctx):
await ctx.send("test")
channel = client.get_channel(1234567890)
await channel.send("test2")
And this works perfectly fine in my "main file", but not in my "extension file", so it's not because of a wrong ID. The await ctx.send("test") works without problems as well, together with any other command I have, just the channel.send is causing troubles.
I'm importing the exact same libraries & co and otherwise should have the exact same "setup" in both files as well.
NoneType error means that it doesn't correctly recognize the channel. When you use get_channel you are looking for the channel in the bot's cache which might not have it. You could use fetch_channel instead - which is an API call.
#client.command()
async def test(ctx):
await ctx.send("test")
channel = await client.fetch_channel(1234567890)
await channel.send("test2")
As you know, the error occurs because your channel is not recognized. The solution is fetch_channel(channel_id). The problem lies in your channel = client.get_channel (1234567890).
Try adding await before client. Next replace get_channel with fetch_channel.
For general use consider your get_channel(), instead you need fetch_channel (channel_id) to retrieve an x.GuildChannel or x.PrivateChannel with the specified ID.
#client.command()
async def test(ctx):
await ctx.send("test")
channel = await client.fetch_channel(1234567890) #update
await channel.send("test2")
Ive made a ghost ping command that pings someone for them to get online, but to avoid spamming, it deletes its own message once all the pings are done.
But the problem is, sometimes people send a message while its still pinging, then after its done, the person's message also gets deleted.
So how do i make the bot only purge it's own message?
heres the code
#client.command()
async def ping(ctx,member: discord.Member,amt):
for i in range(int(amt)):
await ctx.send(f"{member.mention}")
await ctx.channel.purge(limit=int(amt)+1)
Have you read the documentation for purge()? It's quite clearly addressed there, and actually includes some helpful sample code you can use to accomplish such a task using the check parameter. Adapted to your code, it would look something like the following:
def is_me(m):
return m.author == client.user
#client.command()
async def ping(ctx,member: discord.Member,amt):
for i in range(int(amt)):
await ctx.send(f"{member.mention}")
await ctx.channel.purge(limit=int(amt)+1, check=is_me)
You can store the IDs of the messages sent, then use the channel.delete_messages method:
#client.command()
async def ping(ctx, member: discord.Member, amt):
messages = []
for i in range(int(amt)):
m = await ctx.send(f"{member.mention}")
messages.append(m.id)
await ctx.channel.delete_messages(messages)
I'm trying to write a command that deletes multiple messages at once via the message ID.
Here's my (very basic) code so far:
#bot.command()
#commands.has_permissions(administrator=True)
async def delete(ctx, msg:discord.Message):
await msg.delete()
await ctx.message.delete()
The problem with it is that it only deletes the first message. I tried adding something like ,*, before the msg:discord.Message, using for loops and something like message.content.split but so far, everything I tried has been unsuccessful, I'm pretty new to discord bots and python in general.
Thanks in advance for helping out.
You were heading in a good direction, here's how you'd do it:
async def delete(ctx, *messages: discord.Message):
for message in messages:
await message.delete()
Note: This will not work for messages that aren't in the same channel where the command was invoked
i am Making a MassDM command but it dont works well, it sends a Message not to Everyone it stops automatically i think because they turn their DM's off thats why how can i Bypass it? or how can i Fix this error?
Code
#client.command()
async def massdm(self, ctx, *, message):
await ctx.message.delete()
embed = discord.Embed(description=f'**Sent everyone a DM with:** ``{message}``',color=embcolor)
await ctx.send(embed=embed, delete_after=delemb)
for user in list(ctx.guild.members):
try:
await asyncio.sleep(0.1)
await user.send(message)
except:
pass
To debug it properly, the first step would be to remove the try, except block and find out what exceptions are you actually getting. It's a bad habit to catch every exceptions at once. Without knowing what exception is it throwing, I'm unable to help more.
Another thing I would recommend is to benefit from the discord.py being an asynchronous package, and instead of iterating over the users synchronously, try something like that:
coros = [await user.send(message) for user in ctx.guild.members]
await asyncio.gather(*coros)
This way it will send the message to every user at the same time.
EDIT:
I also found another bug while using a loop like that. At some point the bot will try to send the DM to itself, and that is raising AttributeError. Make sure that you handle that, for example:
coros = [user.send(message) for user in ctx.guild.members if user != ctx.guild.me]
await asyncio.gather(*coros)
Maybe that was your issue all along.
Try this:) Hope it helps
#client.command(pass_context=True)
async def massdm(ctx):
await ctx.message.delete()
for member in list(client.get_all_members()):
try:
embed = discord.Embed(title="Test",
description="Test!",
color=discord.Colour.blurple())
embed.set_thumbnail(
url="test")
embed.set_footer(
text=
"test"
)
#delay. Set it to 10 to 20 seconds to avoid flagging
await asyncio.sleep(30)
await member.send(embed=embed)
except:
pass
#await ctx.send(f"Messaged: {member.name}")
print(f"Messaged: {member.name}")