简体   繁体   中英

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:

@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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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