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

Discord联动Google Calendar API:添加事件无标题时间问题求助

问题解决:Discord机器人向Google日历添加带详情的事件

核心问题

你的代码里quickAdd的text参数是空字符串,且未获取用户在命令后输入的事件详情,因此只能创建空事件;同时Discord命令的参数定义需要调整,才能正确接收用户输入内容。

修正后的代码

@bot.command()
async def add(ctx, *, event_details):
    service = get_calendar_service()
    # 传入用户输入的事件详情
    created_event = service.events().quickAdd(
        calendarId='primary',
        text=event_details
    ).execute()
    await ctx.send(f"事件已创建:{created_event['htmlLink']}")
    print(created_event['id'])

关键修改说明

  • async def add(ctx, *, event_details):ctx是Discord.py的上下文对象,*, event_details用于捕获命令后所有输入内容(包含空格),比如用户输入!add 2024-10-06 15:00 产品评审会,event_details会完整接收这段字符串。
  • text=event_details:将用户输入的内容传递给Google Calendar的quickAdd接口,该接口会自动解析标题、时间等信息,生成对应事件。

注意事项

  • 用户输入需符合Google Calendar的识别格式,比如"明天10点 部门例会"、"2024-12-25 圣诞派对"这类表述都能被正确解析。
  • 确保get_calendar_service()已完成Google Calendar API的授权配置,可正常调用接口。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 16:04:59