首页 > 解决方案 > 命令引发异常:AttributeError:“NoneType”对象没有属性“发送”

问题描述

我试图制作一个将消息发送到指定频道并得到以下错误的命令

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

@client.command(name="chmsg")
async def _chmsg(ctx):

    def check(msg):
        return msg.author == ctx.author and \
               msg.channel == ctx.channel

    await ctx.send("Message")
    mestoc = await client.wait_for("message", check=check)
    await ctx.send("Channel id")
    chasend = await client.wait_for("message", check=check)
    mestoch = str(mestoc.content)
    cha = str(chasend.content)
    channel = client.get_channel(cha)
    await channel.send(f"{mestoch}")

标签: discord.py

解决方案


  1. 您不必转换mestoc.content为字符串,因为它已经是字符串

  2. 你需要cha一个整数,而不是一个字符串,这样你就可以得到频道

所以你修改后的代码看起来像

@client.command(name="chmsg")
async def _chmsg(ctx):

    def check(msg):
        return msg.author == ctx.author and \
               msg.channel == ctx.channel

    await ctx.send("Message")
    mestoc = await client.wait_for("message", check=check)
    await ctx.send("Channel ID")
    chasend = await client.wait_for("message", check=check)
    mestoch = mestoc.content
    cha = int(chasend.content)
    channel = client.get_channel(cha)
    await channel.send(f"{mestoch}")

推荐阅读