python - 有没有更简单的方法通过命令进行嵌入? |重写discord.py

标签 python discord.py

我正在设置一个新命令,我想将其放入一个很好的嵌入中。如果每个参数都是 1 个字长,那么它就有效。但是,对于像 dark red 这样的颜色和dark magenta ,它将“dark”视为颜色,“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
            }

最佳答案

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

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))

关于python - 有没有更简单的方法通过命令进行嵌入? |重写discord.py,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56010735/

相关文章:

python - 使用 py2exe 为 Tkinter 程序创建单个 EXE

python 类的属性不在 __init__ 中

python - Discord.py 机器人可以不区分大小写吗?

python - CommandOnCooldown 错误处理程序未捕获错误!已经测试了一切

python - 运行时错误 : Timeout context manager should be used inside a task

python - 如何使用 Python 中的 Discord 机器人获取用户输入?

python - 在 nautilus 中运行 python 脚本

python - 如何在 Pyspark RDD 中查找元素的索引?

python - 使用 django-excel 通过 Excel 工作表上传数据

python - Discord 导入 python 问题