Discord.py Snipe命令

时间:2020-10-16 05:46:20

标签: python discord discord.py-rewrite

我正在尝试发出一条命令,在该命令中,机器人会“剪裁”最近删除的消息。这是我当前的代码:


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

await message.channel.send("Theres nothing to snipe.")部分工作正常,但其余部分将无法工作。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:2)

您的命令现在可能有效,但存在问题。如果我删除服务器中的消息,而您在服务器中运行该命令,您可能会看到该消息。

您应该做的是制作 snipe_message_authorsnipe_message_content 变量字典。

活动应该是这样的:

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.


总而言之,以下是我为您修复的代码:

  • 不显示来自其他服务器的已删除邮件
  • 不显示来自其他频道的已删除邮件
  • 在一台服务器中删除的邮件不会替换在另一台服务器中删除的邮件

希望这有帮助:)

答案 1 :(得分:1)

您的on_message_delete()函数无法正常工作。

我将您的变量缩短为smc(snipe_message_content)和sma(snipe_message_author)。

首先,变量smasmc的类型为None,但是方法removeappend的类型为{ {1}},因此您必须声明列表

list

为了他们的工作。

不过,您还是不必这样做。只需给您当前的变量一个新值即可:

smc = []
sma = []

此外,您不应将60转换为字符串。 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 time.sleep都需要asyncio.sleep才能正常工作。 (顺便说一句,如果您希望60为字符串,只需在integer处加上引号即可。

另外,请注意以下情况:如果删除一条消息,但是在删除一条新消息50秒后,"60"sma将被分配给新消息。但是10秒钟后,之前的消息执行的功能会将smcsma的值设置为None。

因此,smc检查之后,您的消息仍然与以前相同:

await asyncio.sleep(60)