Welcome to dpy-syntaxer’s documentation!

This library will help you with syntax analyzing.

API Reference

Syntax class

class discord.ext.syntaxer.Syntax(command: discord.ext.commands.core.Command, description: Optional[str] = None, *, default_format: str = '{prefix}{variable_prefix}{command_name}{variable_suffix}{suffix}', kwarg_format: str = '{prefix}{variable_prefix}{command_name}{variable_suffix}', required_formats: Tuple[str, str] = ('<', '>'), optional_formats: Tuple[str, str] = ('[', ']'), variable_formats: Tuple[str, str] = ('', '...'))

Analyze command and make Syntax object.

This inherits SpaceList, so :func:str will return a formatted syntax.

Parameters
  • command (commands.Command) – Command to analyze syntax.

  • description (str, optional) – Description of the command. If None, use docstring.

  • default_format (str, optional) – Format of regular argument, by default “{prefix}{variable_prefix}{command_name}{variable_suffix}{suffix}”

  • kwarg_format (str, optional) – Format of keyword argument, by default “{prefix}{variable_prefix}{command_name}{variable_suffix}”

  • required_formats (tuple, optional) – Prefix and suffix of required argument, by default (“<”, “>”)

  • optional_formats (tuple, optional) – Prefix and suffix of optional argument, by default (“[“, “]”)

  • variable_formats (tuple, optional) – Prefix and suffix of variable argument, by default (“”, “…”)

property args

Returns SpaceList with command arguments.

property names

Return SpaceList with command name and parents.

Syntax elements

class discord.ext.syntaxer.CommandName(name: str, parent: Optional[discord.ext.syntaxer.main.ParentName] = None)

Represents name of a command.

Parameters
  • name (str) – Name of the command.

  • parent (ParentName) – Parent of the command, None if the command doesn’t have a parent.

class discord.ext.syntaxer.CommandArgument(name: str, required: bool, description: str, formatted: str, flag: discord.ext.syntaxer.main.ArgumentType, default: Any, param: inspect.Parameter)

Represents a command argument.

str returns formatted value.

Parameters
  • name (str) – Name of the argument.

  • required (bool) – The argument is required or optional.

  • description (str) – Description of the argument.

  • formatted (str) – Formatted value for the argument.

  • flag (int) – Type flag of the argument.

  • default (Optional[Any]) – Default value of the argument if any.

  • param (:class:inspect.Parameter) – Parameter for the argument.

property optional

Same as not self.required.

Other classes

class discord.ext.syntaxer.SpaceList(iterable=(), /)

A special list. When you pass this list to str, content will be converted with :func:str, and joins them with space.

Example

Simple

 1import os
 2from textwrap import dedent
 3
 4import discord
 5from discord.ext import commands, syntaxer
 6
 7bot = commands.Bot(
 8    "b ",
 9    help_command=None,
10    allowed_mentions=discord.AllowedMentions(everyone=False, replied_user=False)
11)
12
13
14@bot.event
15async def on_ready():
16    print(f"Logged in as: {bot.user}")
17
18
19@bot.command("help", aliases=["?"])
20async def _help(ctx, *, command_name=None):
21    """
22    Gets help of the command.
23    Command Name: Command name to get help, return all command name if None.
24    """
25    if command_name is None:
26        return await ctx.reply(" ".join([f"`{c}`" for c in bot.commands]))
27    command = bot.get_command(command_name)
28    if command is None:
29        return await ctx.reply("Unknown command.")
30    syntax = syntaxer.Syntax(command)
31    e = discord.Embed(
32        title=f"Help of `{command}`",
33        description=dedent(f"""
34        ``` {syntax} ```
35        {command.callback.__doc__}
36        """))
37    return await ctx.reply(embed=e)
38
39
40@bot.command()
41async def foo(ctx, argument):
42    """
43    Says bar.
44    Arg: Test argument
45    """
46    await ctx.reply("bar, passed" + argument)
47
48
49@bot.command()
50async def echo(ctx, *, text):
51    """
52    Says text.
53    Text: text to say.
54    """
55    await ctx.send(text)
56
57
58@bot.command()
59async def neko(ctx, count=1):
60    """
61    I am neko.
62    Times: Times to say, default by 1.
63    """
64    for _ in range(count):
65        await ctx.send("Nyan")
66
67bot.run(os.getenv("token"))
_images/simple.png

Customize

Note

Code is as same as first, except help.

19@bot.command("help", aliases=["?"])
20async def _help(ctx, *, command_name=None):
21    """
22    Gets help of the command.
23    Command Name: Command name to get help, return all command name if None.
24    """
25    if command_name is None:
26        return await ctx.reply(" ".join([f"`{c}`" for c in bot.commands]))
27    command = bot.get_command(command_name)
28    if command is None:
29        return await ctx.reply("Unknown command.")
30    syntax = syntaxer.Syntax(
31        command,
32        default_format="{prefix}:{command_name}{variable_suffix}",
33        kwarg_format="{prefix}:{command_name}{variable_suffix}--",
34        required_formats=("R", ""),
35        optional_formats=("O", ""),
36        variable_formats=("", "+")
37    )
38    e = discord.Embed(
39        title=f"Help of `{command}`",
40        description=dedent(f"""
41        ``` {syntax} ```
42        {command.callback.__doc__}
43        """))
44    return await ctx.reply(embed=e)
_images/customize.png

Indices and tables