简体   繁体   English

Discord.py Snipe 命令

[英]Discord.py Snipe command

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. await message.channel.send("Theres nothing to snipe.")部分工作正常,但其余部分不起作用。 Can anyone help?任何人都可以帮忙吗?

Well your on_message_delete() function is just not working.好吧,您的on_message_delete()函数无法正常工作。

I'll shorten your variables as smc (snipe_message_content) and sma (snipe_message_author).我会将您的变量缩短为smc (snipe_message_content) 和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首先,您的变量smasmc的类型为None ,但方法removeappend是类型list一部分,因此您必须声明列表

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.此外,您不应将 60 转换为字符串。 time.sleep and asyncio.sleep both need an integer in order to work. time.sleepasyncio.sleep都需要一个integer ,以工作。 (And by the way, if you wanted 60 to be a string, just write "60" with quotation marks. (顺便说一句,如果你想让 60 成为一个字符串,只需用引号写上"60"

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.另外,请注意以下情况:如果邮件被删除,但新邮件被删除 50 秒后, smasmc将分配给新邮件。 But 10 seconds later, the function executed by the message before would set he value of sma and smc to None.但是 10 秒后,之前消息执行的函数会将smasmc值设置为 None。

Therefore, after await asyncio.sleep(60) check wether your message is still the same as before:因此,在await asyncio.sleep(60)检查您的消息是否仍与以前相同:

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

Your command works now probably, but there's a problem.您的命令现在可能有效,但存在问题。 If I delete a message in my server, and you run the command in your server, you'll probably see the message.如果我删除服务器中的消息,而您在服务器中运行该命令,您可能会看到该消息。

What you should do is make the snipe_message_author and snipe_message_content variables dictionaries.您应该做的是制作snipe_message_authorsnipe_message_content变量字典。

This is how the event should be:事件应该是这样的:

snipe_message_author = {}
snipe_message_content = {}

@client.event
async def on_message_delete(message):
     snipe_message_author[message.channel.id] = message.author
     snipe_message_content[message.channel.id] = message.content
     await sleep(60)
     del snipe_message_author[message.channel.id]
     del snipe_message_content[message.channel.id]

@client.command(name = 'snipe')
async def snipe(ctx):
    channel = ctx.channel
    try: #This piece of code is run if the bot finds anything in the dictionary
        em = discord.Embed(name = f"Last deleted message in #{channel.name}", description = snipe_message_content[channel.id])
        em.set_footer(text = f"This message was sent by {snipe_message_author[channel.id]}")
        await ctx.send(embed = em)
    except: #This piece of code is run if the bot doesn't find anything in the dictionary
        await ctx.send(f"There are no recently deleted messages in #{channel.name}")


#If the bot sends the embed, but it's empty, it simply means that the deleted message was either a media file or another embed.


To summarize it, here's what my code fixes for you:总而言之,这是我的代码为您修复的内容:

  • Doesn't show deleted messages from other servers不显示来自其他服务器的已删除邮件
  • Doesn't show deleted messages from other channels不显示来自其他频道的已删除邮件
  • Deleted message in one server won't replace the deleted message in another server一台服务器中已删除的邮件不会替换另一台服务器中已删除的邮件

Hope this helped :)希望这有帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM