Skip to content

Instantly share code, notes, and snippets.

@15696
Last active May 29, 2023 05:51
Show Gist options
  • Save 15696/3a1b7c80bfe05eacef879d21e74708e2 to your computer and use it in GitHub Desktop.
Save 15696/3a1b7c80bfe05eacef879d21e74708e2 to your computer and use it in GitHub Desktop.
simple custom context in discord.py (requires commands.Bot subclass)
# bot.py
from .context import CustomContext
class Bot(commands.Bot):
"""A subclass of commands.Bot, useful for creating custom context."""
async def get_context(self, message, *, cls = CustomContext):
return await super().get_context(message, cls = cls)
# context.py
class CustomContext(commands.Context):
"""An extended context to use in commands."""
async def test(self):
await self.send("hello!")
# now we can use "await ctx.test()" in our commands
# because ctx gives us our custom context
# get_context tells the bot what context to pass to commands
@Fevzs
Copy link

Fevzs commented Feb 3, 2022

Piç

@xjunko
Copy link

xjunko commented Jan 12, 2023

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment