首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Slash命令编辑bot消息

使用Slash命令编辑bot消息
EN

Stack Overflow用户
提问于 2022-10-06 19:32:54
回答 1查看 93关注 0票数 0

我试图使用消息ID创建一个命令来编辑bot消息,我做了一个前缀,它工作得很好,但是我不知道如何使斜杠命令变得更简单,下面是我尝试的:

代码语言:javascript
复制
@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)

当我尝试使用它时,斜杠命令没有显示任何选项。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-06 23:42:41

命令需要命令上下文的ctx参数。

Pycord在**guild_ids**参数中使用实际的公会ids,而不是在discord.py中为guild使用discord.Objects。

既然你有self参数,我就假设你在齿轮上。您将需要使用@discord.slash_command装饰器。

另外,bot.get_message检查缓存,这意味着它不会一直找到消息。相反,您应该使用await ctx.channel.fetch_message(id),它进行API调用,如果消息存在于当前通道中,则会找到它。

代码语言:javascript
复制
@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)

如果您还有其他关于pycord的问题,最好在支持服务器中询问。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73979173

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档