You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python中同时运行多任务?Discord机器人定时消息实现

给Discord机器人添加每日定时发送消息的实现方案

针对你的需求,这里提供两种可行的实现思路,附修改后的完整代码:

思路一:使用APScheduler异步调度器(推荐)

APScheduler是专门的任务调度库,适配异步运行环境,能轻松实现每日定时任务,还可处理时区、任务重启恢复等问题,很适合Discord.py这类异步框架。

步骤:

  1. 安装APScheduler:
pip install apscheduler
  1. 修改代码,添加调度器初始化和定时任务:
import discord
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from datetime import datetime

intents = discord.Intents.default()
intents.message_content = True

client = discord.Client(intents=intents)
# 替换成你要发送消息的目标频道ID
TARGET_CHANNEL_ID = 123456789012345678
my_secret = 'Here\'s my key'

async def send_daily_message():
    # 获取目标频道对象
    channel = client.get_channel(TARGET_CHANNEL_ID)
    if channel:
        await channel.send("这里是每日12:00自动发送的消息")

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))
    # 初始化异步调度器,替换为你的时区
    scheduler = AsyncIOScheduler(timezone="Europe/Ljubljana")
    # 添加每日12:00执行的定时任务
    scheduler.add_job(send_daily_message, 'cron', hour=12)
    scheduler.start()

@client.event
async def on_message(msg):
    if msg.author == client.user:
        return
    
    # 简化关键词检测逻辑:转小写后直接判断是否包含目标关键词
    content_lower = msg.content.lower()
    if 'zoom' in content_lower and 'kdaj' in content_lower:
        await msg.channel.send('Zoom imamo vsak torek od 16:00 do 17:30')

client.run(my_secret)

思路二:手动用asyncio.sleep实现定时

如果不想依赖第三方库,可以手动计算当前时间到次日12点的时间差,循环执行等待和发送操作:

import discord
import asyncio
from datetime import datetime, timedelta

intents = discord.Intents.default()
intents.message_content = True

client = discord.Client(intents=intents)
TARGET_CHANNEL_ID = 123456789012345678
my_secret = 'Here\'s my key'

async def daily_task():
    await client.wait_until_ready()
    channel = client.get_channel(TARGET_CHANNEL_ID)
    if not channel:
        print("目标频道不存在")
        return
    
    while not client.is_closed():
        # 计算当前时间到下一个12:00的时间差
        now = datetime.now()
        target_time = now.replace(hour=12, minute=0, second=0, microsecond=0)
        if now >= target_time:
            # 今日12点已过,设置为次日12点
            target_time += timedelta(days=1)
        time_diff = (target_time - now).total_seconds()
        
        # 等待到目标时间
        await asyncio.sleep(time_diff)
        # 发送每日消息
        await channel.send("这里是每日12:00自动发送的消息")
        # 额外等待24小时,避免网络延迟导致重复发送
        await asyncio.sleep(86400)

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))
    # 启动定时任务
    client.loop.create_task(daily_task())

@client.event
async def on_message(msg):
    if msg.author == client.user:
        return
    
    content_lower = msg.content.lower()
    if 'zoom' in content_lower and 'kdaj' in content_lower:
        await msg.channel.send('Zoom imamo vsak torek od 16:00 do 17:30')

client.run(my_secret)

注意事项:

  • 两种方案都需要提前获取目标频道ID:开启Discord开发者模式后,右键频道即可复制ID。
  • 时区适配:APScheduler方案可直接指定时区;手动sleep方案需确保服务器时间与你期望的时区一致。
  • 重启恢复:两种方案在机器人重启后都会重新计算任务时间,不会丢失下一次定时发送。

内容的提问来源于stack exchange,提问作者Rok Kužner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 08:15:43