首页 > 解决方案 > Discord.py - 消息在循环中变为“无类型”对象

问题描述

我正在尝试创建一个带有字典的帮助命令,并为带有循环的字典的每个项目创建一些消息编辑,但是当我使用帮助命令AttributeError: 'NoneType' object has no attribute 'edit',它会在涉及 ping 命令时返回错误,我不明白为什么help_msg变成'NoneType'对象......
这是代码:

@bot.command(aliases = ['h','aide'])
async def help(ctx):
    help_msg = await ctx.send("__**Available commands:**__")
    for commande, info in help_list.items():
        help_msg = await help_msg.edit(content=str(help_msg.content)+commande+info)

@bot.command(pass_context=True)
async def ping(ctx, message=None):
    await ctx.send(f":ping_pong: Pong!  `{round(bot.latency*1000)}ms`")


p = bot.command_prefix
help_list = {
    f"``` {p}help | Alias: {', '.join(help.aliases)}```" : "Affiche la liste des commandes | ?help <commande>",
    f"``` {p}ping | Alias: {', '.join(ping.aliases)}```" : "Affiche le ping du bot en ms | ?ping"
    }

标签: discord.py

解决方案


message.edit没有返回值,所以help_msg设置为 none。

我相信如果你只是删除分配它应该工作。

for commande, info in help_list.items():
    await help_msg.edit(content=str(help_msg.content)+commande+info)

推荐阅读