discord.py如何使漫游器在特定时间发送消息以执行命令

时间:2020-08-13 02:04:59

标签: discord.py

我希望我的机器人每天在特定时间运行另一个机器人的命令发送一条消息。 例如,我希望我的机器人每天凌晨2点在特定频道上写“ s!t”,并删除该机器人发送的消息。 我该怎么办?

2 个答案:

答案 0 :(得分:0)

已经有一个关于stackoverflow的问题。请参考here以获取答案

答案 1 :(得分:0)

您可以使用APSchedulerCron安排在特定时间(例如12:00 AM)发送消息

文档:APSchedulerCron

这里是一个例子:

#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()