python - 有没有更简单的方法通过命令进行嵌入?| discord.py 重写
问题描述
我正在设置一个新命令,我想将其放入一个很好的嵌入中。如果每个参数都是 1 个字长,它就可以工作。但是,对于 和 之类的颜色dark red
,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))
推荐阅读
- git - 更改 Atlassian 密码后,我可以使用新密码登录 Bitbucket,但必须在 GIT 中使用旧密码
- python - 如何在python中正确地将列表列表转换为JSON?
- javascript - jwt.decode() 卡住而不是在格式错误的令牌上返回错误
- c++ - C++ 拉普拉斯展开
- python - 使用字符串拆分的列表理解
- python - 使用 numpy silces 优化 3d 数组算法
- shell - grep 多个并找到多个模式
- mysql - 为什么一个外连接的查询要花这么长时间?
- kubernetes - 安全 mTLS 健康检查端点上的 Kubernetes 活跃度探测
- python - 在烧瓶路线上分隔多个单词的约定是什么?