在使用discord.py的 Discord webhook时,set_thumbnail为什么会出现语法错误?
尽管当我不尝试添加 set_thumbnail 时嵌入对象发送得很顺利,但当我试图添加它时却出现错误:
from discord import SyncWebhook, Colour, Embed
import requests
ABwebhook = SyncWebhook.from_url("*url here*")
ABwebhook.send("# caption", embed = Embed(
title = "title",
description= "testing",
url= "https://www.1800flowers.com/articles/flower-facts/most-beautiful-flowers",
color= 16741208,
set_thumbnail(url="https://thumbs.dreamstime.com/b/purple-flower-dewdrops-soft-focus-vibrant-morning-light-nature-beauty-concept-368695089.jpg")
))
使用这段代码会得到:
SyntaxError: positional argument follows keyword argument
然而,如果我尝试:
ABwebhook.send("# caption", embed = Embed(
title = "title",
description= "testing",
url= "https://www.1800flowers.com/articles/flower-facts/most-beautiful-flowers",
color= 16741208,
))
Embed.set_thumbnail(url="https://thumbs.dreamstime.com/b/purple-flower-dewdrops-soft-focus-vibrant-morning-light-nature-beauty-concept-368695089.jpg")
它会报错:
TypeError: Embed.set_thumbnail() missing 1 required positional argument: 'self'
我已经多次查看了相关文档,因此不确定是漏掉了某个导入,还是理解错了。
解决方案
更新:
A comment below pointed out that the documentation indicates:
这个函数返回类实例,以便实现链式风格的连锁调用。
所以你确实可以在把 Embed 实例传给 send() 时就这样内联实现。对 set_thumbnail() 的调用只需要在该实例上完成,而不是作为一个分离的操作。例如:
ABwebhook.send(
"# caption",
embed = Embed(
title = "title",
description = "testing",
url = "https://www.1800flowers.com/articles/flower-facts/most-beautiful-flowers",
color = 16741208
).set_thumbnail(url = "https://thumbs.dreamstime.com/b/purple-flower-dewdrops-soft-focus-vibrant-morning-light-nature-beauty-concept-368695089.jpg")
)
原始答案:
Embed 的实例是匿名的,因为它是在调用 send() 时就地作为参数创建的。所以你没有可用于调用 set_thumbnail() 的引用。
请先创建 Embed 实例。然后你可以在该实例上调用 set_thumbnail(),再将该实例传递给 send()。例如:
#Create the instance
ABembed = Embed(
title = "title",
description = "testing",
url = "https://www.1800flowers.com/articles/flower-facts/most-beautiful-flowers",
color = 16741208
)
#Set the thumbnail on the instance
ABembed.set_thumbnail(url = "https://thumbs.dreamstime.com/b/purple-flower-dewdrops-soft-focus-vibrant-morning-light-nature-beauty-concept-368695089.jpg")
#Use the instance
ABwebhook.send("# caption", embed = ABembed)
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。