如何检查消息内容中的超链接(最好通过 Discord.py) [英] How to check for hyperlinks in the contents of a message (through Discord.py preferably)

查看:23
本文介绍了如何检查消息内容中的超链接(最好通过 Discord.py)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够检测不和谐用户何时发送 URL 链接,无论是视频、网站还是不和谐邀请,并保存链接,是否有任何预先存在的库?

I want to be able to detect when a discord user sends a URL link, whether it be a video, website, or discord invite, and to save the link, are there any pre-existing libraries?

推荐答案

Regex 是我最喜欢的解决方案.

Regex is my favourite solution for this.

    def _match_url(url):
        regex = re.compile(
            "(([w]+:)?//)?(([dw]|%[a-fA-fd]{2,2})+(:([dw]|%[a-fA-fd]{2,2})+)?@)?([dw][-dw]{0,253}[dw].)+[w]{2,63}(:[d]+)?(/([-+_~.dw]|%[a-fA-fd]{2,2})*)*(?(&?([-+_~.dw]|%[a-fA-fd]{2,2})=?)*)?(#([-+_~.dw]|%[a-fA-fd]{2,2})*)?"
        )
        if regex.match(url):
            return True
        else:
            return False

这篇关于如何检查消息内容中的超链接(最好通过 Discord.py)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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