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

PyInstaller打包的Discord自动消息程序在他人电脑上dem()函数失效

Discord自动消息收发程序故障排查

问题现象

我用requests模块开发了一个自动发送(sem())和删除(dem())Discord消息的程序,本地运行正常,打包成可执行文件后在自己电脑上也能正常工作,但发给朋友后,dem()函数停止工作。

程序代码

from pathlib import Path
import requests
import json
from operator import itemgetter
import time

count = 0

txt = Path(__file__).parent / "token.txt"
txt1 = Path(__file__).parent / "channel.txt"
txt2 = Path(__file__).parent / "message.txt"
txt3 = Path(__file__).parent / "timer.txt"
token = txt.read_text()
channel = txt1.read_text()
message = txt2.read_text()
timer = txt3.read_text()

payload = {
    "authorization": token
}

jsondata = {
    "content": message
}

ids = []

def sem():
    requests.post(url=f"https://discord.com/api/v9/channels/{channel}/messages", headers=payload, json=jsondata)

def dem(message_id):
    requests.delete(url=f"https://discord.com/api/v9/channels/{channel}/messages/{message_id}", headers=payload)
    del(ids[-1])
    evaluate()

def evaluate():
    global r
    global i
    r = requests.get(url=f"https://discord.com/api/v9/channels/{channel}/messages", headers=payload)
    i = list(json.loads(r.text))
    i.sort(key=itemgetter("timestamp"))
    for value in i:
        if value['author']['username'] == "username":
            ids.clear()
            ids.append(value['id'])

evaluate()
while True:
    time.sleep(int(timer))
    sem()
    evaluate()
    dem(ids[-1])
    count += 1
    print("Count: "+str(count))

打包与分发方式

我计划制作两个无交互的可执行程序(自动消息程序、手动消息程序)并放在同一文件夹,打包步骤如下:

  • 将自动消息程序打包为onedir模式
  • 将手动消息程序也打包为onedir模式
  • 将第二个程序的_internal文件夹和可执行文件移至第一个程序的文件夹中,不覆盖文件
    程序通过WhatsApp传输给朋友,下载后出现上述问题。

排查与修复建议

  1. 添加API请求错误捕获
    当前代码未处理requests请求的错误,dem()执行失败时没有任何反馈。可以给dem()添加状态码和响应内容的检查,定位具体问题:

    def dem(message_id):
        resp = requests.delete(url=f"https://discord.com/api/v9/channels/{channel}/messages/{message_id}", headers=payload)
        if resp.status_code != 204:
            print(f"删除失败:状态码{resp.status_code},响应内容:{resp.text}")
        else:
            del(ids[-1])
            evaluate()
    
  2. 验证配置文件路径
    打包后Path(__file__).parent在朋友电脑上可能指向错误路径,导致token.txt等配置文件读取失败,进而使authorization无效。可以在程序启动时添加路径检查:

    # 读取配置前添加路径验证
    config_files = [txt, txt1, txt2, txt3]
    for file in config_files:
        if not file.exists():
            print(f"配置文件缺失:{file.resolve()}")
            exit(1)
    
  3. 避免打包依赖冲突
    两个onedir模式的程序合并_internal文件夹后,依赖文件可能出现冲突,导致requests模块运行异常。建议:

    • 将两个程序分别打包为onefile模式
    • 保持两个程序的独立文件夹,不合并_internal目录
  4. 检查ids列表有效性
    如果evaluate()未匹配到目标用户名的消息,ids会为空,调用dem(ids[-1])会直接抛出索引错误。可以添加非空检查:

    while True:
        time.sleep(int(timer))
        sem()
        evaluate()
        if ids:
            dem(ids[-1])
            count += 1
            print(f"Count: {count}")
        else:
            print("未找到可删除的目标消息ID")
    
  5. 确认Discord权限与限制

    • 检查token对应的账号是否拥有目标频道的消息删除权限
    • 确认朋友的网络环境是否触发Discord的Rate Limit(API请求频率限制)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 00:35:20