这就是我目前所知道的..。对于我想要的延迟秒数,is确实可以工作,但我如何添加时间模块或调度模块来使其工作。以防我想让机器人每24小时发送一次消息
import discord
import asyncio
from discord.ext import commands
import schedule
import time
TOKEN = 'xxxxx'
client = commands.Bot(command_prefix = '.')
channel_id = '515994xxxxx5036697'
@client.event
async def on_ready():
print('Bot Online.')
async def alarm_message():
await client.wait_until_ready()
while not client.is_closed:
channel = client.get_channel(channel_id)
messages = ('test')
await client.send_message(channel, messages)
await asyncio.sleep(5) #runs every 5 seconds
client.loop.create_task(alarm_message())
client.run(TOKEN)发布于 2021-09-17 10:38:11
您可以使用discord.ext.tasks来完成此操作。
import discord
import asyncio
from discord.ext import commands
from discord.ext import tasks
import time
TOKEN = 'xxxxx'
client = commands.Bot(command_prefix = '.')
channel_id = '515994xxxxx5036697'
@client.event
async def on_ready():
print('Bot Online.')
@tasks.loop(days=1)
async def alarm_message():
await client.wait_until_ready()
channel = client.get_channel(channel_id)
message = 'test'
await channel.send(message)
alarm_message.start()
client.run(TOKEN)https://stackoverflow.com/questions/53662781
复制相似问题