简体   繁体   English

Pycord - 使用 Slash 命令编辑机器人消息

[英]Pycord - Edit bot messages using Slash Commands

I am trying to make a command to edit a bot message using the message ID, i made a prefix one and it worked well, but im not sure how to make a slash command one to make things easier, here is what i tried:我正在尝试使用消息 ID 发出命令来编辑机器人消息,我做了一个前缀并且效果很好,但我不确定如何使斜杠命令变得更容易,这是我尝试过的:

@bot.slash_command(name="edit", description="Edits the bot messages", guild=discord.Object(id=824342611774144543))
async def edit(self, id: Option(int, description="Message ID", required=True), message: Option(str, description="New Input Message", required=True)):
    msg = self.bot.get_message(id)
    await msg.edit(message)

The slash command shows no options when i try to use it当我尝试使用斜杠命令时,它没有显示任何选项

Commands require a ctx parameter for the context of the command.命令需要一个ctx参数作为命令的上下文。

Pycord uses the actual guild ids in the ** guild_ids ** parameter instead of discord.Object s for guild in discord.py. Pycord 在 ** guild_ids ** 参数中使用实际的公会 ID,而不是 discord.py 中的guild discord.Object s。

Since you have the self parameter, I'm assuming you're in a cog.由于您有self参数,我假设您处于困境。 You'll need to use the @discord.slash_command decorator instead.您需要改用@discord.slash_command装饰器。

Also, bot.get_message checks the cache, which means it won't find the message all the time.此外, bot.get_message检查缓存,这意味着它不会一直找到消息。 You should instead use await ctx.channel.fetch_message(id) which makes an API call and will find the message if it exists in the current channel.您应该改为使用await ctx.channel.fetch_message(id)进行 API 调用,如果消息存在于当前频道中,则会找到该消息。

@discord.slash_command(name="edit", description="Edits the bot messages", guild_ids=[824342611774144543])  # use guild_ids instead
async def edit(
    self,
    ctx: discord.ApplicationContext,  # missing ctx parameter
    id: Option(int, description="Message ID", required=True),
    message: Option(str, description="New Input Message", required=True)
):
    msg = await ctx.channel.fetch_message(id)  # fetch instead
    await msg.edit(message)

If you have other questions about pycord, it's better to ask in the support server .如果你有其他关于pycord的问题,最好在支持服务器中询问。

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

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