我正在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.mands.errors.CommandInvokeError:命令引发异常:AttributeError:"NoneType"对象没有属性"send">
提前谢谢。
正如@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')