简体   繁体   English

discord.py 如何在命令中传递另一个命令

[英]discord.py how to pass another command inside a command

Currently I can change setting using ?hoist yes .目前我可以使用?hoist yes更改设置。

I want to make it if I want to change the settings I have to write ?settings [setting] [value] .如果我想更改必须编写的设置,我想这样做?settings [setting] [value] for example, ?settings hoist yes例如, ?settings hoist yes

@commands.command()
@commands.has_permissions(administrator=True)
async def settings(self, ctx):
    embed=discord.Embed(description="You can use `?[settings] [value]` to change a value of a specific setting. \nfor example, `?createroles yes`", color=0x850000)
    embed.set_author(name='ARB', icon_url='https://i.imgur.com/7ytdI2o.png')
    embed.add_field(name="Create Roles - `createroles`", value="Automatically create roles for games being played by members.", inline=False)
    embed.add_field(name="Mention - `mention`", value="Allow anyone to @mention roles created by ARB.", inline=False)
    embed.add_field(name="Hoist - `hoist`", value="Display roles created by ARB separately from online members.", inline=False)
    embed.set_footer(text="use prefix ? before each command.")
    await ctx.send(embed=embed)

@commands.command()
@commands.has_permissions(administrator=True)
async def hoist(self, ctx, value):
    embed=discord.Embed(color=0x850000)
    embed.add_field(name="hoist", value=f'has been set to `{value}`.', inline=False)
      
    if value.lower() == 'yes':
        self.stg_hoist = True
        await ctx.send(embed=embed)
    elif value.lower() == 'no':
        self.stg_hoist = False
        await ctx.send(embed=embed)
    else:
        await ctx.send(embed=discord.Embed(color=0x850000).add_field(name="hoist", value=f'can not be set to `{value}`'))

According to the discord.py docs ( https://discordpy.readthedocs.io/en/latest/ext/commands/commands.html#positional ) you can have however many positional arguments you want.根据 discord.py 文档( https://discordpy.readthedocs.io/en/latest/ext/commands/commands.html#positional )你可以有很多位置ZDBC11CAA5BDA9E7DZE6 你想要。 So just add some positional arguments.所以只需添加一些位置arguments。

@commands.command()
@commands.has_permissions(administrator=True)
async def settings(self, ctx, setting=None, value=None):
  if setting is None:
    # Whatever your settings was doing before, maybe with
    # updated help text to show new usage
  elif setting == "hoist":
    await self.hoist(ctx, value)

If that doesn't suit you, eg because not all settings have exactly one value, you can use variable number of arguments ( https://discordpy.readthedocs.io/en/latest/ext/commands/commands.html#variable ) and check whether you have what you need based on the specific setting you're changing (eg like this: len(args) >= 1 and args[0] == "hoist" )如果这不适合您,例如因为并非所有设置都只有一个值,您可以使用可变数量的 arguments ( https://discordpy.readthedocs.io/en/latest/ext/commands/commands.html#variable )并根据您正在更改的特定设置检查您是否拥有所需的内容(例如: len(args) >= 1 and args[0] == "hoist"

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

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