Discord.py Snipe command

Willwell

Im trying to make a command where the bot "snipes" the last deleted message. this is my current code:


snipe_message_content = None
snipe_message_author = None

@client.event
async def on_message_delete(message):
    snipe_message_author.remove(None)
    snipe_message_content.remove(None)
    snipe_message_content.append(message.content) 
    snipe_message_author.append(message.author.id) 
    await asyncio.sleep(str(60))
    snipe_message_author.remove(message.author.id)
    snipe_message_content.remove(message.content)
    

@client.command()
async def snipe(message):
    if snipe_message_content==None:
        await message.channel.send("Theres nothing to snipe.")
    else:
        embed = discord.Embed(description=f"{snipe_message_content}")
        embed.set_footer(text=f"Asked by {message.author.name}#{message.author.discriminator}", icon_url=message.author.avatar_url)
        embed.set_author(name= f"<@{snipe_message_author}>")
        await message.channel.send(embed=embed)
        return

the await message.channel.send("Theres nothing to snipe.") part works perfectly fine, but the rest wont work. Can anyone help?

Thörni

Well your on_message_delete() function is just not working.

I'll shorten your variables as smc (snipe_message_content) and sma (snipe_message_author).

First of all, your variables sma and smc are of the type None, but the methods remove and append are part of the type list, so you'd have to declare lists

smc = []
sma = []

in order for them to work.

Still, you wouldn't have to do this anyway. Just give your current variables a new value:

snipe_message_content = None
snipe_message_author = None

@client.event
async def on_message_delete(message):

    global snipe_message_content
    global snipe_message_author
    # Variables outside a function have to be declared as global in order to be changed

    snipe_message_content = message.content
    snipe_message_author = message.author.id
    await asyncio.sleep(60)
    snipe_message_author = None
    snipe_message_content = None

Also, you should not convert 60 to a string. time.sleep and asyncio.sleep both need an integer in order to work. (And by the way, if you wanted 60 to be a string, just write "60" with quotation marks.

Also, be careful of the following case: If a message gets deleted, but 50 seconds after a new message gets deleted, sma and smc would be assigned to the new message. But 10 seconds later, the function executed by the message before would set he value of sma and smc to None.

Therefore, after await asyncio.sleep(60) check wether your message is still the same as before:

snipe_message_content = None
snipe_message_author = None
snipe_message_id = None

@client.event
async def on_message_delete(message):

    global snipe_message_content
    global snipe_message_author
    global snipe_message_id

    snipe_message_content = message.content
    snipe_message_author = message.author.id
    snipe_message_id = message.id
    await asyncio.sleep(60)

    if message.id == snipe_message_id:
        snipe_message_author = None
        snipe_message_content = None
        snipe_message_id = None

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  7. 7

    Do Idle Snowflake Connections Use Cloud Services Credits?

  8. 8

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  9. 9

    Binding element 'string' implicitly has an 'any' type

  10. 10

    BigQuery - concatenate ignoring NULL

  11. 11

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  12. 12

    In Skype, how to block "User requests your details"?

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Pandas - check if dataframe has negative value in any column

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Generate random UUIDv4 with Elm

  17. 17

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  18. 18

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  19. 19

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  20. 20

    EXCEL: Find sum of values in one column with criteria from other column

  21. 21

    How to use merge windows unallocated space into Ubuntu using GParted?

HotTag

Archive