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

Python使用webhook向Discord发送Embed失败,求代码排查及替代方案

代码存在的问题
  • 导入和用法混淆:开头从discord_webhook库导入了类,但实际代码使用的是discord.py库的Webhook、AsyncWebhookAdapter、discord.Embed类,没有对应导入,两个库的API逻辑完全不兼容,直接运行会抛出类未定义的报错。
  • 异步语法错误:异步上下文管理器aiohttp.ClientSession应该使用async with声明,而非普通with;异步发送方法webhook.send没有加await关键字,函数执行时不会等待发送请求完成,请求会直接被丢弃。
  • 变量未声明:代码中使用的embed_title、embed_description、embed_footer_text等自定义变量没有提前赋值,运行时会直接抛出变量未定义异常。
可行实现方式

方式1:使用discord_webhook库实现(同步/异步都支持)

修正后的代码如下:

from discord_webhook import DiscordWebhook, DiscordEmbed
import aiohttp

async def send_webhook():
    webhook_url = '替换为你的webhook链接'
    # 提前给所有自定义变量赋值
    embed_title = "测试标题"
    embed_description = "测试描述"
    embed_footer_text = "页脚文本"
    embed_footer_icon_url = "页脚图标链接"
    embed_main_image = "缩略图链接"
    embed_date = "2024-01-01"
    embed_field_1_name = "字段1"
    embed_field_1_value = "值1"
    embed_field_2_name = "字段2"
    embed_field_2_value = "值2"
    embed_field_3_name = "字段3"
    embed_field_3_value = "值3"
    embed_social_name = "社交信息"
    embed_social_value = "@xxx"

    async with aiohttp.ClientSession() as session:
        webhook = DiscordWebhook(url=webhook_url, session=session)
        # 使用discord_webhook库自带的DiscordEmbed构造消息
        embed = DiscordEmbed(
            title=embed_title,
            description=embed_description,
            color='03b2f8' # 蓝色对应的十六进制色值
        )
        embed.set_footer(text=embed_footer_text, icon_url=embed_footer_icon_url)
        embed.set_thumbnail(url=embed_main_image)
        embed.set_author(name=embed_date)
        embed.add_embed_field(name=embed_field_1_name, value=embed_field_1_value, inline=True)
        embed.add_embed_field(name=embed_field_2_name, value=embed_field_2_value, inline=True)
        embed.add_embed_field(name=embed_field_3_name, value=embed_field_3_value, inline=True)
        embed.add_embed_field(name=embed_social_name, value=embed_social_value, inline=False)
        # 把embed添加到webhook请求中
        webhook.add_embed(embed)
        # 异步发送要加await
        await webhook.execute()
    print('Sent to webhook, discord')

方式2:使用discord.py库实现

如果你要使用discord.py的Webhook相关API,需要先安装discord.py库,修正后的代码如下:

import discord
from discord import Webhook, AsyncWebhookAdapter
import aiohttp

async def send_webhook():
    webhook_url = '替换为你的webhook链接'
    # 提前赋值所有自定义变量
    embed_title = "测试标题"
    embed_description = "测试描述"
    embed_footer_text = "页脚文本"
    embed_footer_icon_url = "页脚图标链接"
    embed_main_image = "缩略图链接"
    embed_date = "2024-01-01"
    embed_field_1_name = "字段1"
    embed_field_1_value = "值1"
    embed_field_2_name = "字段2"
    embed_field_2_value = "值2"
    embed_field_3_name = "字段3"
    embed_field_3_value = "值3"
    embed_social_name = "社交信息"
    embed_social_value = "@xxx"

    async with aiohttp.ClientSession() as session:
        webhook = Webhook.from_url(webhook_url, adapter=AsyncWebhookAdapter(session))
        embed = discord.Embed(
            title = embed_title,
            description = embed_description,
            colour = discord.Colour.blue(),
        )
        embed.set_footer(text=embed_footer_text, icon_url=embed_footer_icon_url)
        embed.set_thumbnail(url=embed_main_image)
        embed.set_author(name=embed_date)
        embed.add_field(name=embed_field_1_name, value=embed_field_1_value, inline=True)
        embed.add_field(name=embed_field_2_name, value=embed_field_2_value, inline=True)
        embed.add_field(name=embed_field_3_name, value=embed_field_3_value, inline=True)
        embed.add_field(name=embed_social_name, value=embed_social_value, inline=False)
        # 异步方法加await
        await webhook.send(embed=embed)
    print('Sent to webhook, discord')

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 14:06:00