【问题标题】:Error sending message to specific channel discord.py向特定频道发送消息时出错 discord.py
【发布时间】:2020-12-03 11:18:50
【问题描述】:

我正在 Discord.py 上制作一个机器人,我正在尝试使用参数设置我想要发送消息的频道。 代码是这样的:

@client.command()
async def send(ctx, arg1):
    channel = client.get_channel(arg1)
    await channel.send('Message')

当我输入频道 id 而不是 arg1 时,它可以工作,但是当我在 Discord (!send 282772187812) 上输入命令时,它不起作用并且我收到此错误:discord.ext.commands.errors.CommandInvokeError:命令引发异常:AttributeError: 'NoneType' object has no attribute 'send'

提前致谢。

【问题讨论】:

  • 您是否尝试过使用try : except: 发送错误消息并尝试恢复执行
  • 不,我没试过
  • discordpy.readthedocs.io/en/latest/api.html 显示频道 id 应该是一个 int,但我相信 arg1 在这里是一个字符串。不知道这是否重要,但您可能想尝试将 arg1 转换为 int。

标签: python discord discord.py


【解决方案1】:

正如@backcab 所说,您可以将arg1 转换为整数或使用来自ext.commands 的通道转换器。

解决方案 1

@client.command()
async def send(ctx, arg1):
    channel = client.get_channel(int(arg1))
    await channel.send('Message')

解决方案 2

@client.command()
async def send(ctx, channel: discord.TextChannel):        
    await channel.send('Message')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 2021-06-19
    • 1970-01-01
    • 2020-07-02
    • 2021-08-21
    相关资源
    最近更新 更多