Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit b584101

Browse files
committed
Implement command groups
+ Debug logger, gettable with logging.getLogger(discord.ext.status) * Context now actually has an awaitable constructor by means of _AsyncInit so no more split between __init__ and _ainit + Context.options replaces options parameter in invoke * Minor fix: Some ext.commands things need Command.cog * Fix: Fetching member also fails when guild is Object * Fix: allowed_mentions was ignored if client.allowed_mentions is None + Implemented subcommands * Context updates self.command and self.options down to the command that is actually executed * Since groups cannot be executed on their own, group coros function as checks instead + Command.qualname includes group names + Command.parent references parent group + class Group + .slash: MAPPING (not set!) of names to subcommands/subgroups + .slash_cmd()(coro), .add_slash, .slash_group, .add_slash_group * Since the structure of a command actually matches that of an option almost perfectly, take advantage of that in to_dict + slash.group()(coro), SlashBot.slash_group, SlashBot.add_slash_group * SlashBot.add_slash_cog allows Groups in cogs too + Command.check is a decorator that adds a cmd-specific check + check can also be passed in init * Renamed Command.method to coro because method implies a class + Option.clone * Fix: reusing the same Option instance used to set the name for all of them * Fix: Option.to_dict didn't call Choice.to_dict + Choice now supports creating from string or dict too * Only start listening to slash commands on_ready - Don't support other interaction data versions * Directly raise an error, not just dispatch it * Completely rework register_commands 1. Check with the API what commands are already registered 2. Create commands found in the bot but not in the API 3. Do not re-register commands in the bot that are identical to the API 4. Update commands that differ between both 5. De-register commands found in the API but not in the bot +demo_bot.py: + Example of how to use the library.
1 parent 0d776e6 commit b584101

File tree

3 files changed

+385
-59
lines changed

3 files changed

+385
-59
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ dist/*
33
__pycache__/
44
build/*
55
*.egg*
6+
.vscode

demo_bot.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import os
2+
import logging
3+
import discord
4+
from discord.ext import slash
5+
6+
#intents = discord.Intents.none()
7+
8+
client = slash.SlashBot(
9+
command_prefix='/', description='', #intents=intents,
10+
debug_guild=337100820371996675
11+
)
12+
13+
@client.slash_cmd()
14+
async def hello(ctx: slash.Context):
15+
"""Hello World!"""
16+
await ctx.respond('Hello World!')
17+
18+
msg_opt = slash.Option(
19+
description='Message to send', required=True)
20+
21+
@client.slash_group()
22+
async def say(ctx: slash.Context):
23+
"""Send a message in the bot's name."""
24+
if 'message' in ctx.options and '@' in ctx.options['message']:
25+
await ctx.respond(embeds=[discord.Embed(title='No mentions!', color=0xff0000)])
26+
return False
27+
28+
emote_opt = slash.Option(
29+
description='Message to send', required=True,
30+
choices=['Hello World!', 'This is a premade message.',
31+
slash.Choice('This will not say what this says.', 'See?')]
32+
)
33+
34+
@say.slash_cmd()
35+
async def emote(ctx: slash.Context, choice: emote_opt):
36+
"""Send a premade message."""
37+
await ctx.respond(choice, allowed_mentions=discord.AllowedMentions.none(),
38+
# sends a message without showing the command invocation
39+
rtype=slash.InteractionResponseType.ChannelMessageWithSource)
40+
41+
@say.slash_cmd()
42+
async def repeat(ctx: slash.Context, message: msg_opt):
43+
"""Make the bot repeat your message."""
44+
await ctx.respond(message, allowed_mentions=discord.AllowedMentions.none(),
45+
# sends a message, showing command invocation
46+
rtype=slash.InteractionResponseType.ChannelMessageWithSource)
47+
48+
@client.slash_cmd()
49+
async def stop(ctx: slash.Context):
50+
"""Stop the bot."""
51+
await ctx.respond(rtype=slash.InteractionResponseType.Acknowledge)
52+
await client.close()
53+
54+
@stop.check
55+
async def check_owner(ctx: slash.Context):
56+
if client.app_info.owner.id != ctx.author.id:
57+
await ctx.respond(embeds=[discord.Embed(title='You are not the owner!', color=0xff0000)])
58+
return False
59+
60+
token = os.environ['DISCORD_TOKEN'].strip()
61+
logging.basicConfig(handlers=[logging.StreamHandler()])
62+
logger = logging.getLogger('discord.ext.status')
63+
logger.setLevel(logging.DEBUG)
64+
65+
try:
66+
client.run(token)
67+
finally:
68+
print('Goodbye.')

0 commit comments

Comments
 (0)