【问题标题】:How do you make cogs in interactions.py?你如何在interactions.py中制作齿轮?
【发布时间】:2022-08-14 11:05:56
【问题描述】:

如何在 discord.py 中制作齿轮,但在 interaction.py 中?

我基本上希望每个命令都有一个单独的 python 文件,并让它们都加载到 main.py 中......但我不知道你在 python 文件中需要什么实际命令。

    标签: discord interaction


    【解决方案1】:

    我们为此使用Extension。这是在 discord.py 中使用 Extension(又名 Cogs)的示例。

    机器人主文件:

    import interactions
    
    client = interactions.Client(...)
    
    client.load("ext1")
    
    client.command(
        name="command_outside",
        description"This command is in main bot file",
    )
    async def _command_outside(ctx: interactions.CommandContext):
        await ctx.send("This command is ran outside of Extension.")
    
    client.start()
    

    ext1.py 文件,它是扩展名,也就是 Cogs。

    import interactions
    
    class Ext(interactions.Extension):
        def __init__(self, client: interactions.Client) -> None:
            self.client: interactions.Client = client
    
        @interactions.extension_command(
            name="command_in_ext",
            description"This command is in an Extension",
        )
        async def _ext_command(self, ctx: interactions.CommandContext):
            await ctx.send("This command is ran inside an Extension")
    
    def setup(client):
        Ext(client)
    

    请注意,您需要更改命令装饰器,因为像 @client.command()@client.component() 这样的东西在 Extension 中不起作用。

    您可以查看文档here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-02
      • 2022-08-14
      • 2011-10-08
      • 2023-04-03
      • 2021-05-12
      • 2011-11-17
      • 2012-08-25
      • 2021-04-12
      相关资源
      最近更新 更多