简体   繁体   English

机器人响应频道提及

[英]Bot responding to channel mentions

I've been trying to develop a discord bot in python recently.我最近一直在尝试在 python 中开发 discord 机器人。 I made it so that if a message contains a certain number, it will react and send a message.我这样做是为了如果一条消息包含某个数字,它会做出反应并发送一条消息。 Here is the code in the cog file:这是cog文件中的代码:

import discord

from discord.ext import commands
    
nice_numbers = ['69', '420']
    
class Auto(commands.Cog):
    def __init__(self, client):
        self.client = client
      
    @commands.Cog.listener()
    async def on_message(self, message):
        
        msg = message.content
    
        if message.author == self.client.user:
             return
    
        if any (word in msg for word in nice_numbers):
             await message.add_reaction('👌')
             await message.channel.send(f'lmao nice')
        
    
def setup(client):
    client.add_cog(Auto(client))

The problem is, the bot also responds with the same message and reaction when a user mentions a certain channel (in this case #general, #super-private-testing, and #resources).问题是,当用户提到某个频道(在本例中为#general、#super-private-testing 和#resources)时,机器人也会以相同的消息和反应进行响应。 I can't seem to fix it or figure out why it's happening.我似乎无法修复它或弄清楚它为什么会发生。 I'm still pretty new to python so can someone please tell me what I'm doing wrong?我对 python 还是很陌生,所以有人可以告诉我我做错了什么吗?

Basically what is happening is that mentions have a special syntax within the Discord API where they are basically a bunch of numbers put together.基本上正在发生的事情是提及在 Discord API 中有一个特殊的语法,它们基本上是一堆数字放在一起。

For example when you are mentioning another user like the following:例如,当您提到另一个用户时,如下所示:

Hello @User1234!你好@User1234!

The real syntax within the discord message is the following: discord 消息中的实际语法如下:

Hello <@125342019199458000>!你好<@125342019199458000>!

And in the case of mentioning channels, it works similar, as a channel mentioned like:在提及频道的情况下,它的工作方式类似,就像提到的频道一样:

#general #一般的

Internally would be written as:内部会写成:

<#550012071928922144> <#550012071928922144>

Of course, the problem is that within this big number there could be false positive of finding your nice_numbers .当然,问题在于,在这个大数字中,找到你的nice_numbers可能是误报。 There could be different ways to avoid this, for example you could check if a channel or a user is being mentioned in the message and return in that case.可以有不同的方法来避免这种情况,例如,您可以检查消息中是否提到了频道用户,并在这种情况下返回。

if message.channel_mentions or message.mentions:
    return

I think a better solution would be to changing the way you are checking if the nice_numbers are within message.content .我认为更好的解决方案是改变您检查nice_numbers是否在message.content内的方式。

Using if word in msg would return true if message.content also includes something like 'My favourite number is 456 69 ' .如果message.content还包含诸如'My favorite number is 456 69 ' 之类的内容if word in msg将返回true To overcome this issue it is better to make use of regular expressions .要克服这个问题,最好使用正则表达式

You can declare a new function like this answers explains, which would return a <match object> if what you pass as parameter is found.您可以像这个答案解释的那样声明一个新的 function ,如果找到您作为参数传递的内容,它将返回一个<match object>

It would be something like this:它会是这样的:

import re

def findCoincidences(w):
    return re.compile(r'\b({0})\b'.format(w)).search

findCoincidences('69')('I like 69')    # -> return <match object>
findCoincidences('69')('My favourite number is 45669')                   # -> return None

Expanding on Shunya's answer , you can use message.clean_content instead of message.content :扩展顺雅的回答,您可以使用message.clean_content代替message.content

A property that returns the content in a “cleaned up” manner.以“清理”方式返回内容的属性。 This basically means that mentions are transformed into the way the client shows it.这基本上意味着提及被转换成客户展示它的方式。 eg <#id> will transform into #name.例如<#id> 将转换为#name。

This will also transform @everyone and @here mentions into non-mentions.这也会将@everyone 和@here 提及转换为非提及。

This will prevent you from inadvertently matching against the IDs of channels, users, roles, etc. Of course, if the actual name contains a nice_number , it'll still match.这将防止您无意中匹配频道、用户、角色等的 ID。当然,如果实际名称包含nice_number ,它仍然会匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM