【问题标题】:Discord bot problem. discord.py AttributeError: 'Bot' object has no attribute 'channel'不和谐机器人问题。 discord.py AttributeError:'Bot'对象没有属性'channel'
【发布时间】:2021-02-11 05:05:28
【问题描述】:

很抱歉今天又发帖了,不过我才刚开始做机器人,有很多问题我挖了全网还是不明白。

我在 embed 中创建了一个表格,每小时上传到提要,但是有错误。

我的问题: AttributeError: 'Bot' object has no attribute 'channel'

这是我的代码:

bot = commands.Bot(command_prefix='.')

@bot.event
async def on_ready():
    crypto_hour.start()
    print('Bot is active!')

@tasks.loop(hours=10)
async def crypto_hour():
    channel = bot.get_channel(809155804170682408)
    embed = discord.Embed(
        title = ":D",
        color = discord.Color.blue(),
    )
    url = 'http://api.coinlayer.com/live?access_key='
    async with aiohttp.ClientSession() as session:
        raw_response = await session.get(url)
        response = await raw_response.text()
        response = json.loads(response)
        embed.set_author(name=':D')
        embed.add_field(name='• **Bitcoin (BTC)**:', value=str(f"{response['rates']['BTC']}PLN"), inline=True)
        embed.add_field(name='• **Ethereum (ETH)**:', value=str(f"{response['rates']['ETH']}PLN"), inline=True)
        await bot.channel.send(embed=embed)

【问题讨论】:

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


    【解决方案1】:

    您需要在您之前定义的channel 上调用send(),而不是在bot.channel 上。因此,您应该将最后一行编辑为:

    await channel.send(embed=embed)
    

    【讨论】:

    • AttributeError: 'NoneType' 对象没有属性 'send'
    • 这意味着没有使用channel = bot.get_channel(809155804170682408)找到具有该ID的频道
    • 但是这个频道存在,我复制它的ID并粘贴。
    • 确保频道存在。另外,试试channel = await bot.get_channel(809155804170682408)
    • 我已经解决了这个问题,而不是 bot.get.channel 我使用了 client.get.channel 并将执行命令的函数替换为 channel.send。会这样吗?
    猜你喜欢
    • 1970-01-01
    • 2019-11-27
    • 2020-12-20
    • 1970-01-01
    • 2021-03-06
    • 2022-10-20
    • 1970-01-01
    • 2020-12-17
    • 2020-03-21
    相关资源
    最近更新 更多