首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Discord.py调度

Discord.py调度
EN

Stack Overflow用户
提问于 2018-12-07 11:25:59
回答 1查看 3.2K关注 0票数 1

这就是我目前所知道的..。对于我想要的延迟秒数,is确实可以工作,但我如何添加时间模块或调度模块来使其工作。以防我想让机器人每24小时发送一次消息

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2021-09-17 10:38:11

您可以使用discord.ext.tasks来完成此操作。

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53662781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档