由我的漫游器发送时,Discord不嵌入链接 [英] Discord does not embed link when sent by my bot

查看:40
本文介绍了由我的漫游器发送时,Discord不嵌入链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码工作正常,并且该漫游器发送了链接,但是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 \\/马耳他语\\/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格式,可以轻松地解析并用作python dict ,使用内置的 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不嵌入链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆