Discord.py 删除消息的命令不起作用

Discord.py command that deletes messages doesn't work

我正在尝试创建一个命令来删除一些我想要的消息,但它不起作用。

我收到这个错误:

'coroutine' object has no attribute 'delete'.

if message.content.startswith("!נקה"):
    delete_count = 0
    try:
        value = message.content.split("!נקה ",1)[1] #gets the value of the messages I want to delete
        value = int(value)
        flag = 1
    except:
        flag = 0
        msg = await message.channel.send("שכחת לכתוב כמה הודעות למחוק.")
        await asyncio.sleep(5)
        await msg.delete()
    if flag == 1:
        for i in range(value-1):
            if True:
                with open("messagesList.txt", "r") as json_file:
                    messagesList = json.load(json_file) #loads a list of the id of messages
                message_id = messagesList[0]
                msg = message.channel.fetch_message(message_id)
                await msg.delete()
                delete_count += 1
                with open("messagesList.txt", "w") as json_file:
                    json.dump(messagesList, json_file)
            else:
                print("", end = "")
        if delete_count != 0:
            message = await message.channel.send(f"{delete_count}הודעות נמחקו בהצלחה.") #prints the messages successfully delete
        await asyncio.sleep(3) #wait 3 seconds
        await message.delete()

错误应该在这里:

  except:
    flag = 0
    msg = await message.channel.send("שכחת לכתוב כמה הודעות למחוק.")
    await asyncio.sleep(5)
    await msg.delete() #The problematic line.

msg 是协程,不是 Message 对象。它没有删除属性。

您可以简单地使用 delete_after=time 参数:

  except:
    flag = 0
    msg = await message.channel.send("שכחת לכתוב כמה הודעות למחוק.", delete_after=5)

另一种方法是让机器人删除邮件,如您所试:

  except:
    flag = 0
    msg = await message.channel.send("שכחת לכתוב כמה הודעות למחוק.")
    await asyncio.sleep(5)
    await bot.delete_message(msg)

中有解释。

您是否正在尝试创建类似 purge 命令的内容?如果你想简单地从频道中删除一些消息(没有其他操作),那么试试这个:

@client.command()
async def clear(ctx, amount = 5): # you can set the default amount of messages that will be deleted (if you didn't specify the amount while running the command)
    deleted = await ctx.channel.purge(limit=amount + 1) # adding one to remove "clear" command too
    await ctx.send(f"Deleted {len(deleted)} messages") # sends how many messages were deleted

Commands in docs