(Discord.py)如何使bot在一段时间后删除自己的消息? [英] (Discord.py) How to make bot delete his own message after some time?

查看:76
本文介绍了(Discord.py)如何使bot在一段时间后删除自己的消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python中有以下代码:

I have this code in Python:

import discord
client = commands.Bot(command_prefix='!')

@client.event
async def on_voice_state_update(member):
    channel = client.get_channel(channels_id_where_i_want_to_send_message))
    response = f'Hello {member}!'
    await channel.send(response)

client.run('bots_token')

并且我希望机器人删除自己的消息.例如,一分钟后,我该怎么办?

And I want the bot to delete its own message. For example, after one minute, how do I do it?

推荐答案

在执行任何操作之前,我们要导入asyncio.这可以让我们等待代码中设置的时间.

Before we do anything, we want to import asyncio. This can let us wait set amounts of time in the code.

import asyncio

首先定义您发送的消息.这样我们可以稍后再使用它.

Start by defining the message you send. That way we can come back to it for later.

msg = await channel.send(response)

然后,我们可以使用asyncio等待一段时间.括号中的时间以秒为单位,因此一分钟将是60,两个将是120,依此类推.

Then, we can wait a set amount of time using asyncio. The time in the parentheses is counted in seconds, so one minute would be 60, two 120, and so on.

await asyncio.sleep(60)

接下来,我们实际上删除了最初发送的消息.

Next, we actually delete the message that we originally sent.

await msg.delete()

因此,您的代码总共最终看起来像这样:

So, altogether your code would end up looking something like this:

import discord
import asyncio

client = commands.Bot(command_prefix='!')

@client.event
async def on_voice_state_update(member, before, after):
    channel = client.get_channel(123...))
    response = f'Hello {member}!'
    msg = await channel.send(response) # defining msg
    await asyncio.sleep(60) # waiting 60 seconds
    await msg.delete() # Deleting msg

您还可以在以下

You can also read up more in this here. Hope this helped!

这篇关于(Discord.py)如何使bot在一段时间后删除自己的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆