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

Discord机器人生成QR码后无法作为消息发送给用户如何解决

Discord Bot无法发送QR码问题修复方案

问题根因

  • qrcode.make()返回的是PilImage图像对象,而discord.File()的fp参数仅支持传入文件路径字符串或者可读类文件对象,直接传入PilImage对象不符合参数要求,因此发送操作失败。

修复方案(内存缓冲区方案,无需生成本地文件)

首先导入Python内置的io模块用于创建内存缓存:

import qrcode
import io
import discord

def create_qr_code(string : str):
    qr = qrcode.make(string)
    # 初始化内存缓冲区
    img_buffer = io.BytesIO()
    # 将QR码以PNG格式写入缓冲区
    qr.save(img_buffer, format='PNG')
    # 重置缓冲区指针到起始位置,避免读取为空
    img_buffer.seek(0)
    return img_buffer


# 发送QR码逻辑
qr_code = create_qr_code('some text')
await ctx.send(file=discord.File(fp=qr_code, filename="my_qr.png"))

可选方案(本地文件方案)

如果需要留存QR码到本地,也可以先保存为本地文件再传入路径:

# 生成阶段保存本地
qr = qrcode.make('some text')
qr.save('local_qr.png')
# 发送阶段直接传文件路径
await ctx.send(file=discord.File('local_qr.png'))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 15:54:08