Discord.py时间表 [英] Discord.py Schedule

查看:0
本文介绍了Discord.py时间表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我到目前为止所拥有的.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)

推荐答案

您可以使用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)

这篇关于Discord.py时间表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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