【问题标题】:discord.py [rewrite] bot not sending message properlydiscord.py [rewrite] bot 未正确发送消息
【发布时间】:2021-03-08 01:07:22
【问题描述】:

最近,我的机器人在更改了与这段代码完全无关的内容后决定停止工作。 当我要求它向消息的作者 (message.author.send()) 发送消息时,它会发送通知,但实际上没有消息出现。我得到带有 1 个 ping 徽章的机器人图标,但是当我单击时,无法读取新消息。以下是相关代码:

    async def on_message(self, Message):
        print('Scanning message')
        blacklisted_words = [
            # I won't show these here for obvious reasons
        ]
        
        
        offendingwords = []
        cursefound = False
        for cursecheck in blacklisted_words:
            if cursecheck in Message.content.lower():
                cursefound = True
                offendingwords.append(cursecheck)
        if cursefound == True:
            print('Found curse word, user notified\n')
            await Message.delete()
            
            words = []
            for word in offendingwords:
                words.append(word)
            
            await Message.author.send(f'Please watch your language in <#{Message.channel.id}>!\nOffending word(s) - `{str(words)}`')
            cursefound = False
        else:
            print('No curse words found\n')

请原谅我糟糕的格式,我对 Python 还是很陌生。

它还会抛出一个 AttributeError:

    await Message.author.send(f'Please watch your language in <#{Message.channel.id}>!\nOffending word(s) - `{str(words)}`')
AttributeError: 'ClientUser' object has no attribute 'send'

请尽量简化这一点,我太容易混淆了。谢谢!

【问题讨论】:

    标签: python url-rewriting notifications discord discord.py


    【解决方案1】:

    如果您想向作者发送 dm,message.author 没有 send() 函数。你可以创建一个函数来做这样的事情:

    async def send_dm(ctx, member: discord.Member, *, message):
        channel = await member.create_dm()
        await channel.send(message)
    

    并将member 作为消息的作者传递。然后将消息作为您要发送的内容传递。这应该会修复您得到的 AttributeError

    【讨论】:

    • 你疯了吗,为什么message.author.send不发私信?
    【解决方案2】:

    这是因为机器人试图发送列表中禁止的相同单词,因此该消息被删除。

    您可以执行以下操作,添加 if 语句以忽略 Bot。

    @bot.event
    async def on_message(Message):
        async def on_message(self, Message):
    
            #If the message author is not a BOT, Then run the following statement Else return
            if not Message.author.bot:
                print('Scanning message')
                blacklisted_words = [
                    # I won't show these here for obvious reasons
                ]
                
                
                offendingwords = []
                cursefound = False
                for cursecheck in blacklisted_words:
                    if cursecheck in Message.content.lower():
                        cursefound = True
                        offendingwords.append(cursecheck)
                if cursefound == True:
                    print('Found curse word, user notified\n')
                    await Message.delete()
                    
                    words = []
                    for word in offendingwords:
                        words.append(word)
                    
                    await Message.author.send(f'Please watch your language in <#{Message.channel.id}>!\nOffending word(s) - `{str(words)}`')
                    cursefound = False
                else:
                    print('No curse words found\n')
    

    【讨论】:

      猜你喜欢
      • 2021-09-10
      • 1970-01-01
      • 2021-07-08
      • 1970-01-01
      • 2021-01-09
      • 1970-01-01
      • 2021-12-25
      • 2021-09-30
      • 1970-01-01
      相关资源
      最近更新 更多