简体   繁体   English

如何编辑消息和重置冷却时间(斜杠命令 discord.py)

[英]How to edit a message and reset cooldown(slash command discord.py)

I've done various searches both in the documentation and in stackoverflow and I haven't found the answer that solves my problem.我在文档和 stackoverflow 中进行了各种搜索,但没有找到解决我问题的答案。 I started programming discord bots again after abandoning it about 2 years and unfortunately with the slash command I'm just having so many problems.在放弃大约 2 年后,我再次开始编写 discord 机器人程序,不幸的是,使用斜线命令我遇到了很多问题。 I've been trying to create a clear command for 3 days and luckily it works... but I can't edit the message on my cooldown so that it continues to lower the seconds for 10 seconds before it deletes the message.我一直在尝试创建一个清晰的命令 3 天,幸运的是它有效......但我无法在我的冷却时间编辑消息,以便它在删除消息之前继续降低秒数 10 秒。 I searched the documentation (precisely here ) and found various answers... too bad no one actually helped me and always gave me this error我搜索了文档(恰好在这里)并找到了各种答案......太糟糕了,没有人真正帮助我并且总是给我这个错误

  File "***", line 116, in clear_error
    await ctx.message.edit(embed=embed)
          ^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'edit'

I then decided to use him and it gave me this error然后我决定使用,它给了我这个错误

discord.errors.InteractionResponded: This interaction has already been responded to before

I'm sure I should do something to make it edit the message always using interaction.response but I'm out of ideas...我确定我应该做些什么来让它始终使用interaction.response编辑消息,但我没有想法......

code:代码:

@app_commands.checks.cooldown(1, 600, key=lambda i: (i.user.id))
@tree.command(name = "clear", description = "Cancella gli ultimi messaggi inviati ", guild=discord.Object(id=guild_id))
async def clear(ctx, limit: int):
    embed = discord.Embed(
        color=0xa61022
    )
    if limit == 0:
        embed.set_author(
            name=f'Non puoi cancellare 0 messaggi!',
        )
        await ctx.response.send_message(embed=embed, delete_after=10.0)
    embed = discord.Embed(
        color=0x03c03c
    )
    if limit == 1:
        embed.set_author(
            name=f'Ho cancellato ufficialmente un messaggio!',
            icon_url=f'{ctx.user.avatar}'
        )
    else:
        embed.set_author(
            name=f'Ho cancellato ufficialmente {limit} messaggi!',
            icon_url=f'{ctx.user.avatar}'
        )
    await ctx.channel.purge(limit=limit)
    await ctx.response.send_message(embed=embed, delete_after=10.0)
    embed = discord.Embed(
        color=0xFFD000
    )
    if limit == 1:
        embed.set_author(
            name=f'{ctx.user.name} ha cancellato un messaggio',
            icon_url=f'{ctx.user.avatar}'
        )
    else:
        embed.set_author(
            name=f'{ctx.user.name} ha cancellato {limit} messaggi',
            icon_url=f'{ctx.user.avatar}'
        )
    embed.add_field(
        name='Messaggi cancellati da:',
        value=f'{ctx.user.name}',
        inline=True
    )
    embed.add_field(
        name='Quantità:',
        value=f'{limit}',
        inline=True
    )
    embed.add_field(
        name='Canale dove è stato eseguito il comando:',
        value=f'{ctx.channel.mention}',
        inline=True
    )
    await logs_moderazione.send(embed=embed)
@clear.error
async def clear_error(ctx, error):
    i = 0
    embed=discord.Embed(
        color=0xa61022
    )
    if isinstance(error, app_commands.CommandOnCooldown):
        cd = round(error.retry_after)
        time = str(datetime.timedelta(seconds=cd))
        embed = discord.Embed(
            description=f"**Riprova tra `{time}`**",
            color=0xa61022
        )
        embed.set_author(
            name=f"Sei in cooldown!",
            icon_url=ctx.user.avatar
        )
        await ctx.response.send_message(embed=embed, delete_after=10.0)
        for i in range(8):
            if isinstance(error, app_commands.CommandOnCooldown):
                cd = round(error.retry_after)
                time = str(datetime.timedelta(seconds=cd))
                embed = discord.Embed(
                    description=f"**Riprova tra `{time}`**",
                    color=0xa61022
                )
                embed.set_author(
                    name=f"Sei in cooldown!",
                    icon_url=ctx.user.avatar
                )
                await ctx.response.edit_message(embed=embed)

Libraries I use: discord.py and datetine global variable: logs_moderazione (is a channel)我使用的库: discord.pydatetine全局变量: logs_moderazione (是一个频道)

the error comes from the last line but it's better to forward all the code.错误来自最后一行,但最好转发所有代码。 I would also like to understand how to reset the cooldown since I can't anymore.我还想了解如何重置冷却时间,因为我不能了。

EDIT: the ctx variable is actually interaction .编辑: ctx变量实际上是interaction

Your edit method is missing, declare the edit method:您的edit方法丢失,声明edit方法:

async def edit(ctx):
    message = await ctx.send('testing')
    await asyncio.sleep(0.3)
    await message.edit(content='v2')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM