【问题标题】:AttributeError: 'NoneType' object has no attribute 'send' Discord.py rewriteAttributeError: 'NoneType' 对象没有属性 'send' Discord.py 重写
【发布时间】:2020-09-15 03:11:32
【问题描述】:

我正在尝试向特定频道发送消息。我仍然无法弄清楚如何做到这一点。非常感谢您的帮助。

import keep_alive
import discord

client = discord.Client()

channel = client.get_channel('ID'`enter code here`)

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('$hello'):
        await channel.send('hello')


keep_alive.keep_alive()

client.run('ID')

【问题讨论】:

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


    【解决方案1】:

    get_channel 无法在您拥有它的地方工作,因为该机器人尚未连接到不和谐(run 期间发生)。当机器人连接时,它将建立它所知道的所有事物(成员、公会、频道等)的内部缓存。这些缓存是各种 get 方法使用的,但由于缓存是空的,这些方法返回 None

    相反,您可以每次都获取on_message 中的频道,或者使用on_ready 中的全局变量:

    @client.event
    async def on_message(message):
        if message.author == client.user:
            return
        if message.content.startswith('$hello'):
            channel = client.get_channel(1234)
            await channel.send('hello')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-27
      • 2023-01-23
      • 2021-05-06
      • 2021-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-19
      相关资源
      最近更新 更多