【问题标题】:Pycord message.content is emptyPycord message.content 为空
【发布时间】:2022-12-09 02:41:05
【问题描述】:

我正在使用 Discord API,发现我无法访问消息的内容。

这是我的代码:

import discord

client = discord.Client()

@client.event
async def on_ready():
    print(f'Logged in as {client.user}')

@client.event
async def on_message(message):
    if 'My Name' in message.author.name:
        print(f'Author: {message.author.name}')
        print(f'Content: {message.content}')
        print(f'Clean_Content: {message.clean_content}')
        print(f'System_Content: {message.system_content}')

client.run(TOKEN, bot=False)

请注意,由于显而易见的原因,令牌和我的用户名在这篇文章中是保密的。

这是我得到的输出,无论消息是什么:

Author: My Name
Content: 
Clean_Content: 
System_Content: 

如您所见,我还尝试了 clean_content 和 system_content 属性。但是,它们都没有显示实际消息。我也尝试过使用机器人帐户,结果出人意料地有效,但我希望它能与我自己的帐户一起使用。问题是 Discord 不打算让私人客户阅读消息,还是我错过了一些基本的东西?

【问题讨论】:

    标签: python discord pycord


    【解决方案1】:

    正如 Matt 提到的,不支持用户机器人。但是,您的问题可能与意图有关。您可以尝试使用以下行:

    import discord
    
    intents = discord.Intents.all()
    
    client = discord.Client(intents=intents)
    

    也可以看看:

    【讨论】:

      【解决方案2】:

      不再支持 Discord selfbots,您可能必须使用仍然支持它们的 discord.ext。似乎使用 discord.py message.content 总是空的。

      【讨论】:

      • 谢谢您的回答。我刚刚阅读了官方 Discord Twitter 帐户的一条推文,其中说不允许使用 selfbots 并将被终止。尽管我显然不打算以有害的方式使用它,但我会尊重他们在此事上的公开立场。然而让我感到困惑的是一些代码仍然有效。例如,发送消息是完全可能的,而阅读消息则不是。不管怎样,我想我的努力到此为止
      【解决方案3】:

      您可以通过在 Discord 开发人员面板中为您的机器人启用 MESSAGE CONTENT INTENT 来解决此问题。

      import os
      TOKEN = os.environ['TOKEN']
      import discord 
      
      
      
      # This example requires the 'message_content' privileged intent to function.
      
      
      class MyClient(discord.Client):
          async def on_ready(self):
              print(f'Logged in as {self.user} (ID: {self.user.id})')
              print('------')
      
          async def on_message(self, message):
              # we do not want the bot to reply to itself
              if message.author.id == self.user.id:
                  return
      
              if message.content.startswith('!hello'):
                  await message.reply('Hello!', mention_author=True)
      
      
      intents = discord.Intents.default()
      intents.message_content = True
      
      
      
      def main():
        client = MyClient(intents=intents)
        client.run(TOKEN)
      
      
      if __name__ == '__main__':
        main()
      

      这是在 documentation 中回复的示例

      【讨论】:

        猜你喜欢
        • 2022-11-03
        • 2022-12-10
        • 1970-01-01
        • 2022-10-05
        • 2021-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-09
        相关资源
        最近更新 更多