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

Discord.py提醒机器人开发:实现用户定时提醒@功能

Discord机器人提醒@用户功能实现

我有一个能回复“good morning”“hello”等消息的Discord机器人,现在需要实现用户设置提醒后机器人@用户的功能。我已经参考大量示例并调试数周,以下是我的代码:

import asyncio


class RemindCommand(commands.Cog):

  def __init__(self, client):
    self.client = client

  @commands.command()
  async def remind(self, ctx, time, *, task):

    def convert(time):
      pos = ['s', 'm', 'h', 'd']

      time_dict = {"s": 1, "m": 60, "h": 3600, "d": 3600 * 24}

      unit = time[-1]

      if unit not in pos:
        #        print("Failed Unit!")
        return -1
      try:
        val = int(time[:-1])

  #     print("Pass Unit!")
      except:
        return -2

        return val * time_dict[unit]

    converted_time = convert(time)

    if converted_time == -1:
      await ctx.send("you didnt imput the time correctly.")
      return

    if converted_time == -2:
      await ctx.send("The number must be an integer")
      return

    await ctx.send(f"Okay, i will remind you to **{task}** at **{time}**.")

    await asyncio.sleep(converted_time)
    await ctx.send(
      f"{ctx.author.mention} your reminder for **{task}** has finished!")


def setup(client):
  client.add_cog(RemindCommand(client))


bot.run('token')

代码问题修正及说明

  1. 缩进错误:convert函数中return val * time_dict[unit]缩进错误,被嵌套在except块内,导致正常场景下无法返回转换后的时间值,需将其移出except块。
  2. 缺失模块导入:代码使用commands.Cog但未导入discord.ext.commands模块,需补充导入语句。
  3. 运行对象不匹配:代码末尾调用bot.run('token'),但类初始化接收的是client实例,应改为client.run('token'),且需先实例化client对象。

修正后的完整代码:

import asyncio
from discord.ext import commands

# 设置机器人命令前缀,替换为你实际使用的前缀
client = commands.Bot(command_prefix="!")

class RemindCommand(commands.Cog):
    def __init__(self, client):
        self.client = client

    @commands.command()
    async def remind(self, ctx, time, *, task):
        def convert(time):
            pos = ['s', 'm', 'h', 'd']
            time_dict = {"s": 1, "m": 60, "h": 3600, "d": 3600 * 24}

            unit = time[-1]
            if unit not in pos:
                return -1
            try:
                val = int(time[:-1])
            except:
                return -2
            # 修正缩进,确保正常返回转换时间
            return val * time_dict[unit]

        converted_time = convert(time)

        if converted_time == -1:
            await ctx.send("时间格式输入错误,请使用如10s、5m、2h、1d这类格式。")
            return
        if converted_time == -2:
            await ctx.send("时间数值必须是整数。")
            return

        await ctx.send(f"好的,我会在**{time}**后提醒你完成**{task}**。")
        await asyncio.sleep(converted_time)
        await ctx.send(f"{ctx.author.mention} 你的**{task}**提醒到了!")

def setup(client):
    client.add_cog(RemindCommand(client))

# 替换为你的机器人Token
client.run('你的机器人Token')

使用说明

  • 将command_prefix的值替换为你机器人实际使用的命令前缀(比如!)。
  • 把你的机器人Token替换为Discord开发者平台获取到的机器人Token。
  • 用户发送!remind 10m 喝水这类命令,机器人会在10分钟后@该用户发送提醒。

内容的提问来源于stack exchange,提问作者punkz

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 18:55:32