client.get_channel(频道编号)returns None

client.get_channel(channelid) returns None

这个问题我在网上搜索了好几次,好像有很多人有这个问题。 然而,他们似乎都通过将表达式放在 on_ready() 事件中来修复它。 但是我想在命令中使用它,用户在其中传递一个频道 ID,它将是一个字符串,我编写了以下代码:

@client.command()
async def send_message(ctx,channel,*,msg):
    channelid = int(channel)
    channel = client.get_channel(channelid)
    await channel.send(msg)

当我 运行 代码并执行命令时,它说:

Command raised an exception: AttributeError: 'NoneType' object has no attribute 'send'

有人知道如何解决这个问题吗?

我认为 channel = client.get_channel(channelid) 行没有返回频道 class。

你把事情弄得有点复杂了。你可以让它更简单。

目前代码本身并没有太大的问题,但是最好使用我们可以使用的工具。

要获取频道,您可以使用 discord.TextChannel 作为参数。

看起来像这样:

channel: discord.TextChannel

您可以在此处使用 ID、频道名称或提及。

你的代码的其余部分起初在我看来是正确的。

您的新密码:

@client.command()
async def send_message(ctx, channel: discord.TextChannel, *, msg):
    await channel.send(msg) # send the "msg" to your specified "channel".