【问题标题】:Discord.py Detect message in Embed Title or DescriptionDiscord.py 检测嵌入标题或描述中的消息
【发布时间】:2021-02-21 19:44:55
【问题描述】:

我目前正在尝试制作“Anti Selfbot”机器人。我想为 Discord 社区做点好事。因此,我尝试创建一个on_message 事件来检测Embed 是否包含“selfbot”,这会导致消息被删除并导致用户被禁止。

我已经开始制作我的机器人了。但是,我不确定如何阅读嵌入的内容。

if 'selfbot' in message.content:
    # do some stuff here

所以,基本上,我目前唯一遇到的问题是阅读嵌入标题或描述内容。

【问题讨论】:

  • 我相信您正在寻找的是查看用户是否发送了嵌入。除非您使用某种类型的被黑/修改过的客户端(这仍然违反不和谐的 TOS),否则只有机器人能够发送嵌入。因此,如果用户发送了嵌入,他们将被禁止。您还需要检查并确保用户不是机器人(实际的机器人不是自我机器人),否则您最终可能会禁止您信任的 dank memer。
  • 澄清一下,如何在某人的消息中找到“selfbot”来验证他们是否正在使用selfbotting?您是否要查明用户帐户是否发送了嵌入?
  • 我认为没有人会在他们进行 selfbotting 时将“selfbot”放入他们的嵌入中

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


【解决方案1】:

下面会检查消息中嵌入的标题、描述、页脚和字段以查找某些文本

from discord import Embed

def message_contains(message, text):
    return text in message.content or any(embed_contains(embed, text) for embed in message.embeds)

def embed_contains(embed, text):
    return (text in embed.title 
         or text in embed.description
         or (embed.footer.text and text in embed.footer.text) 
         or (any(text in field.name or text in field.value for field in embed.fields))
         )

【讨论】:

  • 这是一个很好的答案,但我相信embed.footer 的计算结果为{},如果不存在,embed.footer.text 的计算结果为Embed.Empty。所以embed.footer is Embed.Empty 永远是假的。文档对这种行为不是很清楚。字段的行为也是相同的,除了没有字段评估为空列表 [] 而不是空字典。例如:if embed.footer != {} and embed.footer.text is not discord.Embed.Empty and text in embed.footer.text: return True
  • @Break 文档似乎表明Embed.footer 的类型为Union[EmbedProxy, Empty],这就是我所依据的。我会惊讶地看到一个纯粹的dict 在那里。也许EmbedProxyEmpty 的字符串repr 是{}
  • 如果我们查看set_footer: github.com/Rapptz/discord.py/blob/… 的代码,我们可以看到self._footer 属性本身是一个空字典,它的属性是带有Embed.Empty 的那些。我为这种特殊的嵌入行为苦苦挣扎了一段时间,无法正确使用Embed.Empty,直到我意识到它适用于footer.text 之类的东西,而不是footer 本身。我想空的{} 没关系,因为我们可以只做if embed.footer 而不是if embed.footer != {}
  • 文档甚至源代码 cmets 似乎对这种行为有点误导。我只是说自己尝试一下以验证
  • 只是想回来说你是对的,它不是{},但它也永远不是Embed.Empty,所以支票将改为if embed.footer and embed.footer.text is not discord.Embed.Empty。空的EmbedProxyrepr 也是'EmbedProxy()',所以另一个愚蠢但准确的检查可能是if repr(embed.footer) != 'EmbedProxy()'。对于空支票:if repr(embed.footer) == 'EmbedProxy()'if not embed.footerif len(embed.footer) == 0
猜你喜欢
  • 2020-08-28
  • 2021-04-09
  • 2019-01-12
  • 2019-05-15
  • 2018-01-18
  • 1970-01-01
  • 2021-01-23
  • 2021-02-13
相关资源
最近更新 更多