首页 > 解决方案 > url 检测 discord.py

问题描述

    urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+',message.content.lower())
    if urls and message.channel.id == "702468264361590828":
        await message.channel.send("Links are not allowed in here")

这是我们的 discord.py 机器人的代码,你们在上面看到的命令应该检测是否发送了链接,然后发送消息。但它没有检测到它。我已经print代替了,await message.channel.send("Links are not allowed in here")但这也没有用。我什么都进口了。仅供参考,我用它来导入re

pip install regex

我找不到一个名为的包re,所以我使用了这个

标签: pythondiscord.py

解决方案


您不需要导入任何包。re 包(正则表达式)随附已导入的 Python 3。

您的问题是由频道 ID 的字符串版本引起的。频道 ID始终采用 int 形式。所以只需删除周围的引号"702468264361590828"


推荐阅读