mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-20 18:06:08 -05:00
[V3] Custom context class (#988)
* Create custom context class * Documentation * Remove old help method, replace with new * Update from rebase
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import pkg_resources
|
||||
|
||||
from .config import Config
|
||||
from .context import RedContext
|
||||
|
||||
__all__ = ["Config", "__version__"]
|
||||
__all__ = ["Config", "RedContext", "__version__"]
|
||||
|
||||
__version__ = version = pkg_resources.require("Red-DiscordBot")[0].version
|
||||
|
||||
@@ -10,8 +10,7 @@ from discord.ext import commands
|
||||
from discord.ext.commands import GroupMixin
|
||||
|
||||
from .cog_manager import CogManager
|
||||
from . import Config
|
||||
from . import i18n
|
||||
from . import Config, i18n, RedContext
|
||||
|
||||
|
||||
class Red(commands.Bot):
|
||||
@@ -84,15 +83,8 @@ class Red(commands.Bot):
|
||||
return True
|
||||
return await super().is_owner(user)
|
||||
|
||||
async def send_cmd_help(self, ctx):
|
||||
if ctx.invoked_subcommand:
|
||||
pages = await self.formatter.format_help_for(ctx, ctx.invoked_subcommand)
|
||||
for page in pages:
|
||||
await ctx.send(page)
|
||||
else:
|
||||
pages = await self.formatter.format_help_for(ctx, ctx.command)
|
||||
for page in pages:
|
||||
await ctx.send(page)
|
||||
async def get_context(self, message, *, cls=RedContext):
|
||||
return await super().get_context(message, cls=cls)
|
||||
|
||||
async def shutdown(self, *, restart=False):
|
||||
"""Gracefully quits Red with exit code 0
|
||||
|
||||
42
redbot/core/context.py
Normal file
42
redbot/core/context.py
Normal file
@@ -0,0 +1,42 @@
|
||||
"""The purpose of this module is to allow for Red to
|
||||
further customise the command invocation context provided
|
||||
by discord.py.
|
||||
"""
|
||||
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
|
||||
__all__ = ["RedContext"]
|
||||
|
||||
TICK = "\N{WHITE HEAVY CHECK MARK}"
|
||||
|
||||
|
||||
class RedContext(commands.Context):
|
||||
"""
|
||||
Command invocation context for Red.
|
||||
|
||||
All context passed into commands will be of this type.
|
||||
|
||||
This class inherits from
|
||||
:py:class:`commands.Context <discord.ext.commands.Context>`.
|
||||
"""
|
||||
|
||||
async def send_help(self):
|
||||
"""Send the command help message."""
|
||||
command = self.invoked_subcommand or self.command
|
||||
pages = await self.bot.formatter.format_help_for(self, command)
|
||||
for page in pages:
|
||||
await self.send(page)
|
||||
|
||||
async def tick(self):
|
||||
"""Add a tick reaction to the command message.
|
||||
|
||||
:return: ``True`` if adding the reaction succeeded.
|
||||
:rtype: bool
|
||||
"""
|
||||
try:
|
||||
await self.message.add_reaction(TICK)
|
||||
except discord.HTTPException:
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
@@ -136,7 +136,7 @@ class Core:
|
||||
async def _set(self, ctx):
|
||||
"""Changes Red's settings"""
|
||||
if ctx.invoked_subcommand is None:
|
||||
await ctx.bot.send_cmd_help(ctx)
|
||||
await ctx.send_help()
|
||||
|
||||
@_set.command()
|
||||
@checks.guildowner()
|
||||
@@ -207,7 +207,7 @@ class Core:
|
||||
try:
|
||||
status = statuses[status.lower()]
|
||||
except KeyError:
|
||||
await ctx.bot.send_cmd_help(ctx)
|
||||
await ctx.send_help()
|
||||
else:
|
||||
await ctx.bot.change_presence(status=status,
|
||||
game=game)
|
||||
@@ -229,7 +229,7 @@ class Core:
|
||||
game = discord.Game(type=1, url=streamer, name=stream_title)
|
||||
await ctx.bot.change_presence(game=game, status=status)
|
||||
elif streamer is not None:
|
||||
await ctx.bot.send_cmd_help(ctx)
|
||||
await ctx.send_help()
|
||||
return
|
||||
else:
|
||||
await ctx.bot.change_presence(game=None, status=status)
|
||||
@@ -267,7 +267,7 @@ class Core:
|
||||
async def prefix(self, ctx, *prefixes):
|
||||
"""Sets Red's global prefix(es)"""
|
||||
if not prefixes:
|
||||
await ctx.bot.send_cmd_help(ctx)
|
||||
await ctx.send_help()
|
||||
return
|
||||
prefixes = sorted(prefixes, reverse=True)
|
||||
await ctx.bot.db.prefix.set(prefixes)
|
||||
|
||||
@@ -74,9 +74,9 @@ def init_events(bot, cli_flags):
|
||||
@bot.event
|
||||
async def on_command_error(ctx, error):
|
||||
if isinstance(error, commands.MissingRequiredArgument):
|
||||
await bot.send_cmd_help(ctx)
|
||||
await ctx.send_help()
|
||||
elif isinstance(error, commands.BadArgument):
|
||||
await bot.send_cmd_help(ctx)
|
||||
await ctx.send_help()
|
||||
elif isinstance(error, commands.DisabledCommand):
|
||||
await ctx.send("That command is disabled.")
|
||||
elif isinstance(error, commands.CommandInvokeError):
|
||||
|
||||
Reference in New Issue
Block a user