【问题标题】:Editing a bot's message just like when you swipe a page, discord.py像滑动页面一样编辑机器人的消息,discord.py
【发布时间】:2021-03-01 07:38:55
【问题描述】:

我想通过另一个命令创建一个编辑机器人旧消息的命令,例如:

user: !cmds
bot: *commands list*

user: !next
bot: *edits old message into a new one*

我自己尝试了很多次,但总是失败,谁能帮帮我?

我是如何做到的:

@client.command
async def test():
  embed=discord.Embed(title="Page1")
  await ctx.send(embed=embed)

@client.event
async def on_command(message):
    embed2=discord.Embed(title="Page2")
    await client.process_commands(message)
    if message.content == '$next':
        await message.edit(content=embed2)

【问题讨论】:

  • 请添加minimal, reproducible example,如果我们为其他人编写代码,那么这不是一个网站
  • 好吧,对不起,我试着让它变成这样:@client.command async def test(): embed=discord.Embed(title="Page1") await ctx.send(embed=embed) @client.event async def on_command(message): embed2=discord.Embed(title="Page2") await client.process_commands(message) if message.content == '$next': await message.edit(content=embed2)

标签: discord.py message editing


【解决方案1】:

每当调用命令时都会调用on_command 事件,您没有注册next 命令,这是第二个on_command 不采用message 参数,它采用@987654325 @(ctx)。

制作您所要求的内容的一个想法是拥有一个带有引用上一条消息的实例变量的 cog,然后在调用 next 命令时对其进行编辑。

class SomeCog(commands.Cog):
    def __init__(self, client):
        self.client = client
        self._previous_message = None


    @commands.command()
    async def test(self, ctx):
        message = await ctx.send("I'll edit this message when you type `$next`")
        self._previous_message = message


    @commands.command()
    async def next(self, ctx):
        if self._previous_message is None: # Exiting if the `test` command wasn't called before, i.e the message is a NoneType
            return await ctx.send("You need to invoke first `$test`")

        await self._previous_message.edit(content="Edited the message")


client.add_cog(SomeCog(client))

我猜你可以使用全局变量或机器人变量来做到这一点,但在我看来,这是最好的方法。

【讨论】:

    猜你喜欢
    • 2020-10-10
    • 1970-01-01
    • 2020-08-16
    • 2020-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-05
    • 2023-03-21
    相关资源
    最近更新 更多