【问题标题】:Discord.py ScheduleDiscord.py 时间表
【发布时间】:2019-05-08 19:43:11
【问题描述】:

这就是我到目前为止所拥有的......对于我想要的延迟秒数确实有效,但是如何添加时间模块或 shedule 模块以使其工作.. 以防万一我希望机器人每 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)

【问题讨论】:

  • 您可以使用您提供的代码,但您可以使用time模块在特定时间发布消息,而不是使用asyncio.sleep
  • 可以使用日程库schedule.readthedocs.io/en/stable
  • 我该怎么做??我希望它每 24 小时发送一次消息

标签: python python-3.x scheduled-tasks python-asyncio discord.py


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2021-05-15
    • 1970-01-01
    • 2021-05-04
    • 1970-01-01
    • 2021-05-02
    • 2021-04-21
    • 2021-10-16
    • 1970-01-01
    相关资源
    最近更新 更多