【问题标题】:Discord.py send messageDiscord.py 发送消息
【发布时间】:2021-07-08 03:30:23
【问题描述】:

我在下面有一个示例代码块,我正在尝试运行它以向特定频道发送消息。当我运行它时,我得到这个错误:AttributeError:'NoneType'对象没有属性'send'。我知道频道 ID 很好。这很简单,但我知道 discord.py,所以我可能会忽略一些东西

import discord
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')

intents = discord.Intents(guild_messages=True)
client = discord.Client(intents=intents)

@client.event
async def on_ready():
    await client.get_channel(811074101505425418).send("bot is online")

client.run(TOKEN)

【问题讨论】:

  • 试试client.channel.send("bot is online")
  • @Goion AttributeError: 'Client' 对象没有属性 'channel' 是我得到的
  • 重新检查您的令牌并检查您是否拥有正确的权限。好像不能认证什么的
  • @Goion 我踢了机器人,重新设置了开发者页面上的权限,但我仍然没有运气......对于如此直截了当的事情来说似乎真的很奇怪
  • print(client.user.name) 打印什么吗?

标签: python discord.py


【解决方案1】:

你可以试试这个

Channel=‘#channelID’
Await channel(‘#your message’)

【讨论】:

    【解决方案2】:

    你可以试试这个:

    @client.event
    async def on_ready():
        # get channel by ID and save it in the variable "channel"
        channel = await client.get_channel(811074101505425418)
        # send your message to the channel
        await channel.send('bot is ready')
    

    【讨论】:

      【解决方案3】:

      我认为您的问题是您在使用时定义了频道。 这应该有效:

      from dotenv import load_dotenv
      
      load_dotenv()
      TOKEN = os.getenv('DISCORD_TOKEN')
      GUILD = os.getenv('DISCORD_GUILD')
      
      intents = discord.Intents(guild_messages=True)
      client = discord.Client(intents=intents)
      
      @client.event
      async def on_ready():
      #make a channel variable first
          channel = client.get_channel(811074101505425418)
      #if you want to use the message later eg. to delete it you can make a variable too
          message = await channel.send('Bot is online!')#only channel.send because the channel 
      #is a variable
      
      client.run(TOKEN)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-10
        • 2020-10-31
        • 2017-08-15
        • 2021-05-02
        • 2021-10-15
        • 1970-01-01
        • 2020-10-15
        • 2021-05-06
        相关资源
        最近更新 更多