【问题标题】:How to use interactions.py in cogs?如何在cogs中使用interactions.py?
【发布时间】:2022-08-14 11:00:55
【问题描述】:

我将如何在 cogs 中使用interactions.py?我希望每个命令都有不同的文件,但我不确定如何使用interactions.py 来做到这一点。

    标签: python python-3.x discord discord.py


    【解决方案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)
    

    您可以查看文档here

    【讨论】:

      猜你喜欢
      • 2019-04-30
      • 2023-01-22
      • 1970-01-01
      • 2022-08-14
      • 1970-01-01
      • 2021-04-19
      • 2019-09-21
      • 2022-11-28
      • 2020-12-12
      相关资源
      最近更新 更多