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

使用discord.py的Discord Webhook调用set_thumbnail时出现语法错误怎么办?

解决Discord Webhook中Embed缩略图的两个错误问题

错误原因分析

1. SyntaxError: positional argument follows keyword argument

你错误地将set_thumbnail()方法作为参数传入Embed的构造函数中,Embed构造函数仅接受title、description、color这类属性参数,并不支持传入set_thumbnail。同时你的写法违反了Python语法规则——位置参数(set_thumbnail(url=...)的调用)出现在了关键字参数(如title="xxx")之后,导致语法报错。

2. TypeError: Embed.set_thumbnail() missing 1 required positional argument: 'self'

set_thumbnail是Embed的实例方法,必须通过已创建的Embed实例来调用。直接用Embed.set_thumbnail()是调用类方法,缺少实例本身这个self参数,因此触发错误。

正确写法示例

根据你使用的库,分两种情况:

情况1:使用第三方discord_webhook库

import discord
from discord_webhook import DiscordWebhook, DiscordEmbed

# 1. 创建Embed实例
embed = DiscordEmbed(title="测试标题", description="测试描述")
# 2. 通过实例调用set_thumbnail方法
embed.set_thumbnail(url="https://你的缩略图链接")

# 发送Webhook
webhook = DiscordWebhook(url="你的Webhook链接")
webhook.add_embed(embed)
response = webhook.execute()

情况2:使用discord.py自带的Webhook模块

import discord
from discord import Webhook, AsyncWebhookAdapter
import aiohttp

async def send_webhook():
    async with aiohttp.ClientSession() as session:
        webhook = Webhook.from_url("你的Webhook链接", adapter=AsyncWebhookAdapter(session))
        # 1. 创建Embed实例
        embed = discord.Embed(title="测试标题", description="测试描述")
        # 2. 通过实例调用set_thumbnail方法
        embed.set_thumbnail(url="https://你的缩略图链接")
        await webhook.send(embed=embed)

注意事项

  • 确认导入的Embed类正确:第三方库用DiscordEmbed,discord.py自带的用discord.Embed,不要混淆。
  • 缩略图链接必须是可公开访问的URL,不能用本地文件路径。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.01 19:33:13