简体   繁体   English

如何一起使用两个 async def

[英]How do I use two async def together

I am using the code below for sniping deleted messages我正在使用下面的代码来狙击已删除的消息

@client.event
async def on_message_delete(message):
    if message.attachments:
        bob = message.attachments[0]
        client.sniped_messages[message.guild.id] = (
        bob.proxy_url, message.content, message.author, message.channel.name, message.created_at)
    else:
        client.sniped_messages[message.guild.id] = (
        message.content, message.author, message.channel.name, message.created_at)

But when I add the code below, the snipe command stops working, is the a way for that to not happen?但是当我添加下面的代码时,狙击命令停止工作,这是一种不会发生的方法吗?

@client.event
async def on_message_delete(message):
    embed = discord.Embed(
        timestamp=message.created_at,
        title = "[Message Deleted]",
        colour = discord.Colour(0xff0000)
        )
    embed.set_author(name=f'{message.author.name}#{message.author.discriminator}', icon_url=message.author.avatar_url)
    embed.set_footer(text=f"Author ID:{message.author.id} • Message ID: {message.id}")
    embed.add_field(name="Message Content:", value=message.content)
    embed.set_image(url=message.attachments[0].url)
    channel = client.get_channel(825939423061475359)
    await channel.send(embed=embed)

You can only have one event, you have to combine them both.你只能有一个事件,你必须将它们结合起来。

@client.event
@client.event
async def on_message_delete(message):
    if message.attachments:
        bob = message.attachments[0]
        client.sniped_messages[message.guild.id] = (
        bob.proxy_url, message.content, message.author, message.channel.name, message.created_at)
    else:
        client.sniped_messages[message.guild.id] = (
        message.content, message.author, message.channel.name, message.created_at)

    embed = discord.Embed(
        timestamp=message.created_at,
        title = "[Message Deleted]",
        colour = discord.Colour(0xff0000)
        )
    embed.set_author(name=f'{message.author.name}#{message.author.discriminator}', icon_url=message.author.avatar_url)
    embed.set_footer(text=f"Author ID:{message.author.id} • Message ID: {message.id}")
    embed.add_field(name="Message Content:", value=message.content)
    embed.set_image(url=message.attachments[0].url)
    channel = client.get_channel(825939423061475359)
    await channel.send(embed=embed)

If you're using commands.Bot you can create listeners with the Bot.listen() decorator:如果您使用commands.Bot ,您可以使用Bot.listen()装饰器创建侦听器:

@client.listen()
async def on_message_delete(message):
    embed = discord.Embed(
        timestamp=message.created_at,
        title = "[Message Deleted]",
        colour = discord.Colour(0xff0000)
    )
    embed.set_author(name=f'{message.author.name}#{message.author.discriminator}', icon_url=message.author.avatar_url)
    embed.set_footer(text=f"Author ID:{message.author.id} • Message ID: {message.id}")
    embed.add_field(name="Message Content:", value=message.content)
    embed.set_image(url=message.attachments[0].url)
    channel = client.get_channel(825939423061475359)
    await channel.send(embed=embed)


@client.listen()
async def on_message_delete(message):
    if message.attachments:
        bob = message.attachments[0]
        client.sniped_messages[message.guild.id] = (
        bob.proxy_url, message.content, message.author, message.channel.name, message.created_at)
    else:
        client.sniped_messages[message.guild.id] = (
        message.content, message.author, message.channel.name, message.created_at)

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

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