首页 > 解决方案 > 有没有更简单的方法通过命令进行嵌入?| discord.py 重写

问题描述

我正在设置一个新命令,我想将其放入一个很好的嵌入中。如果每个参数都是 1 个字长,它就可以工作。但是,对于 和 之类的颜色dark reddark magenta它会将“深色”视为颜色,将“洋红色”视为标题,然后将其后的所有内容视为值。

我认为我可以让它工作的唯一方法是命令让你做一些k!embed <colour>, <title>, <value>用逗号分隔的事情,但我不知道有什么方法可以做到这一点。我尝试用谷歌搜索它,但很可能是由于缺乏术语而一无所获。此外,添加更多星号似乎没有帮助......这是我最后一次绝望的努力。

@client.command(name='embed',
                aliases=['e'],
                pass_ctx=True)
async def embed(ctx, colour, name, *, value):

    # making paramater match dictionary
    colour = colour.lower()
    colour.replace(' ', '_') 

    # checking if colour is valid
    if colour not in colours:
        await ctx.send('Invalid colour')
        return
    else:
        colour = colours[colour]
        # sets colour
        embed = discord.Embed(
            color = colour()
            )  
    # adds other paramaters
    embed.add_field(name='{}'.format(name), value="{}".format(value), inline=False)

    # final product
    await ctx.send(embed=embed)
    print('Embed executed\n- - -')

正如我所提到的,输入类似的东西k!embed dark magenta title this is the value会完全丢失,我更喜欢类似的东西k!embed dark magenta, title, this is the value或类似的东西。谢谢!

编辑:对于上下文,这是colours字典和标题错字:

colours = { "red" : discord.Color.red,
            "dark_red" : discord.Color.dark_red,
            "blue" : discord.Color.blue,
            "dark_blue" : discord.Color.dark_blue,
            "teal" : discord.Color.teal,
            "dark_teal" :discord.Color.dark_teal,
            "green" : discord.Color.green,
            "dark_green" : discord.Color.dark_green,
            "purple" : discord.Color.purple,
            "dark_purple" :discord.Color.dark_purple,
            "magenta" : discord.Color.magenta,
            "dark_magenta" : discord.Color.dark_magenta,
            "gold" :discord.Color.gold,
            "dark_gold" : discord.Color.dark_gold,
            "orange" :discord.Color.orange,
            "dark_orange" :discord.Color.dark_orange
            }

标签: pythondiscord.pydiscord.py-rewrite

解决方案


这是一个自定义转换器,即使没有通过使用未解析参数中的另一个单词来引用颜色,它也应该能够识别颜色:

from discord.ext.commands import Converter, ArgumentParsingError
from discord import Color, Embed

class ColorConverter(Converter):
    async def convert(self, ctx, argument):
        argument = argument.lower()
        if argument in ('dark', 'darker', 'light', 'lighter'):
            ctx.view.skip_ws()
            argument += "_" + ctx.view.get_word().lower()
        if not hasattr(Color, argument):
            ctx.view.undo()
            raise ArgumentParsingError(f"Invalid color {argument}")
        return getattr(Color, argument)()

@bot.command()
async def test(ctx, color: ColorConverter, *, text):
    await ctx.send(embed=Embed(color=color, description=text))

推荐阅读