我的代码运行良好,机器人发送了链接,但 Discord 无法识别它,也没有嵌入它.当我自己复制并粘贴它时,它会将其识别为链接并嵌入图像.这是我的代码:
My code works fine and the bot sends the link, but Discord does not recognize it as one and does not embed it. When I copy and paste it myself, it then recognizes it as a link and embed the image. Here is my code:
import requests
from bs4 import BeautifulSoup
if message.content.startswith(".dog"):
response = requests.get("https://dog.ceo/api/breeds/image/random")
soupRaw = BeautifulSoup(response.text, 'lxml')
soupBackend = str(soupRaw).split("message")
soup2 = soupBackend[1]
soup3 = soup2[3:]
soup = soup3[:-20]
await bot.send_message(message.channel, soup)
这里是一个例子:https://imgur.com/m9GM2wQ
有谁知道如何让它在我的机器人发送链接时嵌入链接?感谢您的帮助!
Does anyone know how to make it embed the link when it is sent by my bot? Thanks for the help!
我不是在尝试发送嵌入消息,而是在尝试发送将被 Discord 嵌入的链接,如我的示例所示.这不是重复的问题.
I am not trying to send an embedded message, I am trying to send a link that will BE embedded by Discord, as shown in my example. This is not a duplicate question.
很晚的答案,但如果有人在稍后查看此答案;我认为问题在于您的变量 soup
包含每个正斜杠的转义字符(反斜杠),例如https:\/\/images.dog.ceo\/breeds\/maltese\/n02085936_4480.jpg
.
Very late answer, but if anyone is viewing this answer for later; I believe the problem is that your variable soup
contains escape characters (backward slashes) for every forward slash, e.g. https:\/\/images.dog.ceo\/breeds\/maltese\/n02085936_4480.jpg
.
将此作为消息发送(即使作为普通用户)会显示链接的外观,但不会自动为其创建嵌入.您可以使用函数 soup.replace("\", "")
替换反斜杠.
Sending this as a message (even as a regular user) shows the link as it should look, but does not automatically create an embed for it. You can replace the backslashes with the function soup.replace("\", "")
.
然而,我会推荐一种完全不同的方法,因为 response
对象有一个 .content
属性,它是 json 格式,可以很容易地被解析,并用作 pythondict
,使用内置的 import json
库(它会自动为您留下转义码,您不必担心 response.text 的字符串长度).
I would however recommend a completely different approach as the response
object has a .content
attribute which is in json format and can easily be parsed, and used as a python dict
, using the builtin import json
library (It will automatically leave the escape codes for you and you don't have to worry about string length for response.text).
soup = json.loads(response.content).get("message")
应该可以解决问题.
这篇关于当我的机器人发送时,Discord 没有嵌入链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!