【问题标题】:Discord Bot Error trying to send to a unique channel IDDiscord Bot 错误尝试发送到唯一的频道 ID
【发布时间】:2022-01-10 19:10:45
【问题描述】:

当我尝试向频道发送消息时,它不起作用。有谁知道为什么?

这是我正在使用的命令。这只是不工作部分的代码块。

@bot.command(pass_context=True)
    async def test2(ctx):
    await ctx.message.delete()
    print("The Test is being started")
    Channel = config['bot']['id1']
    await Channel.send("test2")

在我的配置文件中,我有这个

[bot]
id1= 916845047CENSORED

当我尝试运行它时,我得到了这个错误:

The test is being started
Ignoring exception in command test2:
Traceback (most recent call last):
  File "C:\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "c:/Users/jp/Desktop/DISCORD BOT/bot.py", line 224, in test2
    await Channel.send("test2")
AttributeError: 'str' object has no attribute 'send'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'str' object has no attribute 'send'

如果有人知道如何解决这个问题,我会非常感谢你


编辑:

解决方法是:

如果它来自 txt 文件/配置,则将 int 添加到频道 ID

@bot.command(pass_context=True)
async def test2(ctx):
    await ctx.message.delete()
    Channel_Id = config['bot']['id1']
    print("Test is being started")
    Chanel = bot.get_channel(int(Channel_Id))
    await Chanel.send("test2")

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    查看第 85 行以找到引发错误的行。就是说取自config['bot']['id1']Channel 对象属于str 类型。然后,您尝试在 str 类型上调用方法 .send(),它没有。

    ...
    Channel = config['bot']['id1']
    await Channel.send("test2")
    

    【讨论】:

    • 我从其他人那里听说这是问题所在,但我如何让它成为有效的 str?
    【解决方案2】:

    对于discord.pychannel objects 不仅仅是整数。相反,您必须获取频道,要么在获取公会及其 id 后从公会获取,要么立即获取频道。这里有一些方法可以做到这一点:

    Channel_Id = config['bot']['id1']
    
    # Based on your error, ensure to convert Channel_Id to an Integer
    # Example: "123454" -> 123454
    Channel_Id = int(Channel_Id)
    
    # Method 1: Fetching channel straight away
    Channel = bot.get_channel(Channel_id)
    
    # Method 2: Fetching channel from guild
    Guild_Id = config['bot']['GuildId'] # of course you may need to add a new config
    # Remember to convert this to an Integer as well!
    Guild_Id = int(Guild_Id)
    Guild = bot.get_guild(Guild_Id)
    Channel = Guild.get_channel(Channel_Id)
    
    await Channel.send("Test")
    

    其他有用的链接:

    请注意:在搜索此答案时,建议不要使用bot.fetch_channel,因为这是一个 API 调用。以上方法推荐用于一般用途。


    编辑:当使用bot.get_channel 或类似的东西时,您可能需要Intents

    此类问题的链接:

    【讨论】:

    • 您好,很遗憾这不起作用我更正了我的代码。我仍然遇到同样的错误
    • @bot.command(pass_context=True) async def test2(ctx): await ctx.message.delete() Channel_Id = config['bot']['id1'] Guild_Id = config['bot']['gu1'] print("Test is being started") Guild = bot.get_guild(Guild_Id) Channel = Guild.get_channel(Channel_Id) await Channel.send("test2") 这也不起作用,我得到这个错误:`discord.ext.commands.errors.CommandInvokeError:命令引发异常:AttributeError:'NoneType'对象没有属性'get_channel'
    • 只是为了确定,Channel_IdGuild_Id 都是整数吗?还是它们是字符串下的数字?还要检查公会 ID 是否有效,例如打印 Guild.name 或类似的东西。您也可以尝试使用方法 1,因为这通常对我有用。 @TheDEV
    • 尝试使用此代码:Channel_Id = config['bot']['id1'] Guild_Id = config['bot']['gu1'] print("Test is being started") print("Channel ID: " + str(bot.get_channel(Channel_Id))) print("Guild ID: " + str(bot.get_guild(Guild_Id))) print("Guildname: " + str(bot.get_guild(Guild_Id).name)) print("Chanelname: " + str(bot.get_channel(Channel_Id).name)) 但从外观上看,它无法获取公会名称或香奈儿名称,它说没有,而是说:NoneType' object has no attribute 'name'
    • 非常感谢解决问题的方法就是添加这个:Channel_Id = (int(Channel_Id))
    【解决方案3】:

    解决方法是:

    如果它来自 txt 文件/配置,则将 int 添加到频道 ID

    @bot.command(pass_context=True)
    async def test2(ctx):
        await ctx.message.delete()
        Channel_Id = config['bot']['id1']
        print("Test is being started")
        Chanel = bot.get_channel(int(Channel_Id))
        await Chanel.send("test2")```
    

    【讨论】:

      猜你喜欢
      • 2018-11-24
      • 2021-07-20
      • 2021-01-10
      • 2018-11-24
      • 2023-03-07
      • 2020-08-25
      • 2023-03-27
      • 2020-07-13
      • 1970-01-01
      相关资源
      最近更新 更多