【问题标题】:client.fetch_channel() returns AttributeError: '_MissingSentinel' object has no attribute 'is_set'client.fetch_channel() 返回 AttributeError: \'_MissingSentinel\' 对象没有属性 \'is_set\'
【发布时间】:2023-01-19 19:07:22
【问题描述】:

我想向特定频道发送消息。我得到了频道,但是当我尝试发送消息时出现此错误:

Traceback (most recent call last):
  File "C:\Users\nikit\PycharmProjects\Phantom_Shop\discord\ui\view.py", line 425, in _scheduled_task
    await item.callback(interaction)
  File "C:\Users\nikit\PycharmProjects\Phantom_Shop\temporary_classes.py", line 200, in buy_callback
    await channel.send("Test")
AttributeError: 'NoneType' object has no attribute 'send'

我在一个单独的文件中执行此操作(特别是在视图中的按钮回调中) 这是使用的单独文件的一部分:

from temporary_bot import client
channel = client.get_channel(channel_id)
await channel.send("Test")

这是主要文件:

client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)

channel = client.get_channel(1062092063128166571)

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


@tree.command(name = "start", description = "Start the bot")
async def first_command(interaction):
    view = Menu_View()
    view.add_item(url)
    keys = data["profiles"]

    if f"{interaction.user}" in keys:
        print("Account already in database")
    else:
        data["profiles"][f"{interaction.user}"] = {}
        data["profiles"][f"{interaction.user}"]["balance"] = 0
        print("This is bullshit")
        data["profiles"][f"{interaction.user}"]["spent"] = 0
        data["profiles"][f"{interaction.user}"]["bought_product"] = 0
    with open("profiles.json", "w") as file:
        json.dump(data, file)
    menu = make_menu(interaction.user.avatar)
    await interaction.response.send_message(embed=menu, view=view)

if __name__ == "__main__":
    client.run(TOKEN)

我尝试在主文件中创建变量,但它犯了同样的错误。 我使用一个数字作为我的频道 ID,我在其他频道上尝试过,同样的事情。

【问题讨论】:

    标签: python discord


    【解决方案1】:

    您正在尝试在机器人启动之前获取频道。尝试在 on_ready 中设置它:

    global channel
    channel = client.get_channel(1062092063128166571)
    

    或者最好只是在你的回调中得到它

    channel = client.get_channel(1062092063128166571)
    await channel.send("Test")
    

    如果要将其保存在全局变量中,最好使用带有 id 的类常量变量

    CHANNEL_ID = 1062092063128166571
    
        # Somewhere in a callback...
        channel = client.get_channel(CHANNEL_ID)
        await channel.send("Test")
    

    【讨论】:

      猜你喜欢
      • 2015-12-30
      • 1970-01-01
      • 1970-01-01
      • 2022-07-08
      • 2014-03-24
      • 2018-03-20
      • 1970-01-01
      • 1970-01-01
      • 2012-12-01
      相关资源
      最近更新 更多