Last active
September 8, 2024 05:47
-
-
Save 15696/3a1b7c80bfe05eacef879d21e74708e2 to your computer and use it in GitHub Desktop.
simple custom context in discord.py (requires commands.Bot subclass)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Piç