【问题标题】:discord.py how to make the bot send a message at a specific time to execute a commanddiscord.py 如何让机器人在特定时间发送消息以执行命令
【发布时间】:2020-12-02 19:50:08
【问题描述】:

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

【问题讨论】:

    标签: discord.py


    【解决方案1】:

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

    【讨论】:

    • 我不知道如何使用 scheduler.add_job 配置 02.00。
    • hour="2", minute="0", second="0"
    • 我尝试使用关闭时间来查看它是否有效(例如 19.28),但我不喜欢它,我不明白为什么
    • ok 工作但不执行命令。我认为通过让机器人编写命令来执行命令是不可能的。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-03
      • 2020-11-08
      • 1970-01-01
      • 2019-07-27
      • 2022-12-01
      • 2021-01-26
      • 1970-01-01
      • 2021-06-02
      相关资源
      最近更新 更多