【发布时间】:2020-12-02 19:50:08
【问题描述】:
我希望我的机器人在每天的特定时间发送一条消息,运行另一个机器人的命令。 例如,我希望我的机器人每天凌晨 2 点在特定频道上写“s!t”,并让机器人发送的消息被删除。 我该怎么办?
【问题讨论】:
标签: discord.py
我希望我的机器人在每天的特定时间发送一条消息,运行另一个机器人的命令。 例如,我希望我的机器人每天凌晨 2 点在特定频道上写“s!t”,并让机器人发送的消息被删除。 我该怎么办?
【问题讨论】:
标签: discord.py
您可以使用 APScheduler 和 Cron 来安排您的消息在特定时间发送,例如 12:00 AM
文档:APScheduler、Cron
这是一个例子:
#async scheduler so it does not block other events
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger
from discord.ext import commands
import discord
bot = commands.Bot(command_prefix="!")
async def func():
c = bot.get_channel(channel_id)
await c.send("s!t")
@bot.event
async def on_ready():
print("Ready")
#initializing scheduler
scheduler = AsyncIOScheduler()
#sends "s!t" to the channel when time hits 10/20/30/40/50/60 seconds, like 12:04:20 PM
scheduler.add_job(func, CronTrigger(second="0, 10, 20, 30, 40, 50"))
#starting the scheduler
scheduler.start()
【讨论】:
已经有一个关于这个的 stackoverflow 问题。答案请参考here
【讨论】: