【问题标题】:Screenshot command Discord.py rewrite截图命令 Discord.py 重写
【发布时间】:2020-05-03 13:01:20
【问题描述】:

首先,我是一个新人,这是我的第一个问题,所以我想请你们忽略这个问题中的任何缺陷或意想不到的细节。

所以我正在尝试为我的 Discord.py 机器人制作一个 screenshot 命令,目前我对以下代码片段感到震惊:

async def ss(ctx, site):
    embed=discord.Embed(colour = discord.Colour.orange(), timestamp=ctx.message.created_at)
    embed.set_image(url=(f"https://image.thum.io/get/width/1920/crop/675/maxAge/1/noanimate/{site}"))
    await ctx.send(embed=embed)

但是,即使是有效的 URL,机器人也只会发送一个空嵌入。目前,对我来说最明显的是 Discord 无法将其识别为有效图像,因为它没有以 .png 或 .jpeg 之类的图像扩展名结尾,因此是空嵌入。

TBH 我不知道我想要实现的任何替代代码。我搜索了很多,我认为这与 BytesIO 有关,但我对如何使用该模块实现这一点一无所知。

我现在期待的是两件事:

  1. 修复当前缺陷,以便我能够发送所需网站的屏幕截图。
  2. 如果网站无效,则向消息作者报告,即指定域上没有网站,或者由于网站端响应延迟而导致请求超时。

因此,我想请求社区帮助我实现此命令的目标。我不是要求被喂食,但这是迄今为止我的机器人中唯一的命令,对此我一点也不知道如何解决它。我要提前感谢大家的体贴答复。

衷心的问候,
萨扬·巴塔查亚。

【问题讨论】:

  • 您在set_image 行上似乎有一个不匹配的括号。
  • 感谢您指出这一点。这是我的错,因为我正在使用我的手机,这是在复制和粘贴代码片段时发生的。这在我的机器人代码中实际上是正确的。尽管如此,嵌入是空的。
  • 您是否有示例 site 可以使用此代码?
  • 是的,以 StackOverFlow 本身为例,如下所示:image.thum.io/get/width/1920/crop/675/maxAge/1/noanimate/https:/…

标签: python python-3.x discord discord.py-rewrite


【解决方案1】:

我最终做了一些研究和调试,发现该东西不适用于前面没有 HTTPS:// 或 HTTP:// 的 URL。如果我使用包含它们的链接,它可以正常工作

那么现在你必须做什么

  • 查看用户是否在链接开头使用了 http:// 或 https://
if not site.startswith("https://", "http://"):
    # site doesn't start with http:// or https://
    site = "https://" + site # we add https in front of the URL
  • 查看链接是否有效
# add this at the top of your file
import re

# define this variable somewhere outside of the command
URL_REGEX = re.compile(r"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+")

# then in your command, use
if not re.fullmatch(URL_REGEX, site):
    # URL is invalid, send a message here
    await ctx.send("Invalid URL")
    return 
    # the `return` here stops the command from running after sending the message

【讨论】:

    猜你喜欢
    • 2020-10-19
    • 2021-07-15
    • 2020-11-06
    • 2021-03-30
    • 2020-09-11
    • 2021-04-04
    • 2021-09-14
    • 2019-07-10
    • 2021-06-15
    相关资源
    最近更新 更多