While I was coding for help command for my bot in Discord.py, This function was not working as I expected.
def syntax(command):
cmd_and_aliases = '|'.join([str(command), *command.aliases])
params = []
for key, value in command.params.items():
if key not in ('self', 'ctx'):
params.append(f'[{key}]' if 'NoneType' in str(value) else f'<{key}>')
params = ' '.join(params)
return f'`{cmd_and_aliases} {params}`'
What I wanted for this function was, returning and printing names and aliases to call up an discord command and what parts are necessary and what parts are not to run that command properly. As the Optional[str] in Typing can be translated as Union[str, None], What I wanted to get was what their could be and if it can be None, then it will be appended to the params list wrappend in square bracket, or else in angle bracket.
`name|aliases <necessary part> [optional part]`
This is what the function would originally would have returned, but both the necessary part and optional part are shown as wrapped in angle brackets.
@command(name='slap', aliases=['hit'])
async def slap_member(self, ctx, member: Member, *, reason: Optional[str] = 'for no reason'):
For example, for this command the result would have been like below.
slap|hit <Member> [reason]
But what I ended was getting was:
slap|hit <Member> <reason>.
Is there something I have done wrong or is there any update that would have made this function broken? If there is something I did wrong, please help me by showing what I can do otherwise.