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

使用discum实现DM私信内按钮自动点击代码运行失效求助

discum 私信场景按钮自动点击失效修复方案

你写的代码存在5个直接导致点击失效的问题,逐个说明:

  • 冗余拉取消息+数据取值错误:网关事件返回的resp.parsed.auto()已经包含完整的消息结构,不需要额外调用bot.getMessage重复请求接口。私信场景下bot.getMessage返回的结构不是数组,你写的message.json()[0]本身就会触发索引错误,根本拿不到正确的组件数据。
  • 交互请求目标参数错误:bot.click的第一个参数是guildID,私信场景不属于任何服务器,这个位置必须传None,你传消息作者ID会把交互请求发到错误的端点,服务端会直接丢弃请求。
  • 按钮匹配逻辑鲁棒性差:buts.getButton("First")只能取第一行组件里标签为"First"的按钮,只要消息里第一行是下拉菜单、或者第一个按钮是不可点击的链接按钮,就会返回空值,点击请求没有有效载荷自然不会生效。
  • 缺少自消息过滤:网关会推送当前账号自己发送的消息,不加判断会触发无意义的点击逻辑,甚至出现请求死循环。
  • 缺少场景过滤:默认会处理所有频道的消息,不符合你只处理DM私信的需求,还容易在公共频道误触发操作。

修复后可直接运行的代码

import discum
from discum.utils.button import Buttoner

# 替换为你的账号Token
bot = discum.Client(token='YOUR_DISCORD_TOKEN', log=False)

@bot.gateway.command
def auto_click_dm_button(resp):
    if resp.event.message:
        msg = resp.parsed.auto()
        # 跳过自己发的消息
        if msg['author']['id'] == bot.gateway.session.user['id']:
            return
        # 仅处理私信频道(频道类型为1代表DM)
        if msg.get('channel_type') != 1:
            return
        # 无按钮组件的消息直接跳过
        if 'components' not in msg or not msg['components']:
            return

        channel_id = msg['channel_id']
        message_id = msg['id']
        message_flags = msg.get('flags', 0)
        btn_handler = Buttoner(msg['components'])

        # 遍历组件找第一个可点击的普通按钮(排除链接按钮)
        target_btn = None
        for component_row in msg['components']:
            for comp in component_row.get('components', []):
                # type=2是按钮类型,style=5是跳转链接按钮,不支持交互点击
                if comp['type'] == 2 and comp.get('style') != 5:
                    target_btn = btn_handler.getButton(comp['label'])
                    break
            if target_btn:
                break
        
        if not target_btn:
            return

        # 私信场景guildID传None
        bot.click(
            None,
            channelID=channel_id,
            messageID=message_id,
            messageFlags=message_flags,
            data=target_btn
        )

if __name__ == '__main__':
    # 开启自动重连
    bot.gateway.run(auto_reconnect=True)

运行前注意事项

  • 先升级discum到最新版本,旧版本对私信场景的交互签名计算有误,会返回401鉴权失败,执行pip install discum --upgrade即可升级
  • 如果你要点击的是指定标签的按钮,直接把遍历找按钮的逻辑替换成btn_handler.getButton("你要点击的按钮文本")即可
  • discum是用户态第三方客户端,违反Discord用户协议,使用存在账号封禁风险,不建议在常用主号上运行
  • 部分限时验证类按钮会绑定会话、时间戳校验,如果按钮已过期,就算请求格式正确也会被服务端拒绝

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 20:01:20