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