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)
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 am only trying to learn how to "read" the reaction on a message the same bot sends. I've been stuck for days. I looked it up but the only tutorials I find are with a specific message for roles. I don't care about that, I won't use a single message for the whole server, so there is no way I can get the ID for the message. I only want the bot to send a message, then the user reacts and the bot writes "you reacted with [emoji]".
I found some questions on this site, but they only managed to confuse me even more. Still, this is what I barely managed to make.
#bot.command()
async def react(ctx):
await ctx.send("React to me!")
#bot.event
async def on_reaction_add(reaction, user):
await channel.send("{}, you responded with {}".format(user, reaction))
There is actually a good example of this already in the documentation, you can find it here:
wait_for
But to simplify the whole thing a bit, here is a sample code:
#bot.command()
async def react(ctx):
def check(reaction, user): # Our check for the reaction
return user == ctx.message.author # We check that only the authors reaction counts
await ctx.send("Please react to the message!") # Message to react to
reaction = await bot.wait_for("reaction_add", check=check) # Wait for a reaction
await ctx.send(f"You reacted with: {reaction[0]}") # With [0] we only display the emoji
Here is how it looks like:
Without reaction[0] you would only get unnecessary information. [0] shows you only the first digit, in this case Reaction emoji.
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 trying to code a command into my Discord Bot that when triggered will actively delete new messages from a specific user.
I have tried a couple variations of using ctx but I honestly I don't entirely understand how I could use it to accomplish this.
This block of code deletes the message that triggers it. Because of this, I think I am taking the wrong approach because it only deletes from whoever triggers it and only if they type the command. Obviously, I am new to this. I would appreciate any and all help. Thanks so much :)
#client.event
async def on_message(ctx):
message_author = ctx.author.id
if message_author == XXXXXXXXXXXXXXXXX:
await ctx.message.delete()
There is no more CTX in discord rewrite (DiscordPy 1.0+) instead you should do:
#client.event
async def on_message(message):
if message.author.id == XXXXXXXXXXXXXXXXX:
await message.delete()
Source: link
You can make a command to add the users to a list then use on_message to check if a user is in that list and then delete it if true
dels=[]
#bot.event
async def on_message(msg):
if msg.author.id in dels:
await bot.delete_message(msg)
await bot.process_commands(msg)
#bot.command(name='del_msg')
async def delete_message(con,*users:discord.Member):
for i in users:
dels.append(i.id)
await con.send("Users {} have been added to delete messages list".format(" ,".join(users)))
I created a super simple .report <person> command. I have it so it will be sent to a certain channel when someone types it. What I wanted to do is have it display the name of the user who reported the other user. I don't know how to go about doing that. Does anyone know the best way?
#bot.command()
async def report(*, message):
await bot.delete(message)
await bot.send_message(bot.get_channel("479177111030988810"), message)
You could do something like this, where you take the context (ctx) and from it get the contents of the message and its author
#bot.command(pass_context=True)
async def report(ctx):
await bot.delete_message(ctx.message)
report = f"\"{ctx.message.content[8:]}\" sent by {ctx.message.author}"
await bot.send_message(bot.get_channel("479177111030988810"), report)
You can use ctx.author.mention for mentioning the user. The mention attribute (documented here) returns a string that you can use in the argument to send.
Example usage:
#bot.command("hello", help="Learn about this bot")
async def hello(ctx: discord.commands.Context):
await ctx.send(f"Hi {ctx.author.mention}! I'm a bot!")