Package: discord.ext.context
discord.ext.contextA globally accessible context object for discord.py events.Able to be used in both base clients and the commands extension.Installationpipinstalldiscord.ext.contextRequirementsPython 3.7+discord.py 1.6+Register a Discord bot/clientSubclass thecontext.ContextClientbase class, ensuring that it's first in inheritance order.importdiscordfromdiscord.extimportcontextclassClient(context.ContextClient,discord.Client):...Using EventContextImportcontext.ctxfrom anywhere, and it'll have its attributes set based on the event that your call stack originates from.importdiscordfromdiscord.ext.contextimportctxasyncdeflog_reaction():awaitctx.channel.send(f"{ctx.user}reacted with{ctx.emoji}")client=discord.Client()@client.eventasyncdefon_raw_reaction_add():awaitlog_reaction()ExceptionsContextNotSetAccessing a context value before it's set will have this exception raised. To have a fallback value used instead, use the contextmanagerwith ctx.default():.Attributesctx.message:discord.PartialMessageShould always be aPartialMessage. If aMessageinstance is needed, an up to date copy can be retrieved withPartialMessage.fetch().ctx.emoji:discord.Emojiordiscord.PartialEmojiOften representing a reaction interacted with by a user; useful for user interactions that use reaction-based sessions.ctx.channel:discord.abc.MessageableWill always be a text channel or user-type object that's possible to send messages to. Does not include voice channels.ctx.user:discord.Memberordiscord.UserIf setting adiscord.Userinstance and the user shares only a single guild with the client, it'll replace it with thediscord.Memberinstance.ctx.guild:discord.GuildWill only be set on guild-specific events.ctx.cmd_ctx:discord.ext.commands.ContextWill only be set on the command event, with other EventContext values being set using it.ctx.event:strThe name of the event type this context originates from.ctx.client:discord.ClientThe instance of the discord.py client being hooked into.ctx.bot:discord.ClientAlias forctx.client.Methodsctx.set(*, message=None, emoji=None, user=None, channel=None, guild=None)Sets the values for the current context to be used across future call stacks. Won't impact asynchronous calls from other events.@ctx.register_hook(event)Decorator for registering an event to be handled by the decorated function. Will override existing hooks if a duplicate exists.ctx.default(all_default, *, message=_NoValue, emoji=_NoValue, user=_NoValue, channel=_NoValue, guild=_NoValue, cmd_ext=_NoValue)Context manager for registering default values to be used if a value isn't set. On leaving the context manager's scope,ctxwill revert to original state.Useall_defaultto set all the available ctx.values to the one value. This can be useful for allowing None to be returned for nonset contexts.Exampleswithctx.default(None):ifctx.channel:awaitctx.channel.send("Yes")withctx.default(channel=fallback_channel,user=None):ifctx.user:awaitctx.channel.send(f"{ctx.user.display_name}")Ifctx.channelorctx.useris not yet set, it'll be assigned the fallback arguments. This includes being able to set a value toNoneinstead of raisingContextNotSet.It can also be used as a decorator for a function.@ctx.default(channel=fallback_channel,user=None)asyncdefshow_name():ifctx.user:awaitctx.channel.send(f"{ctx.user.display_name}")ctx.ephemeral(*, message=_NoValue, emoji=_NoValue, user=_NoValue, channel=_NoValue, guild=_NoValue, cmd_ext=_NoValue)Context manager for overriding context values. On leaving the context manager's scope,ctxwill revert to original state.Exampleswithctx.ephemeral(channel=log_channel,user=None):ifctx.user:awaitctx.channel.send(f"{ctx.user.display_name}did a thing.")Ifctx.channelorctx.useris not yet set, it'll be assigned the fallback arguments. This includes being able to set a value toNoneinstead of raisingContextNotSet.It can also be used as a decorator for a function.@ctx.ephemeral(channel=log_channel,user=None)asyncdefshow_name():ifctx.user:awaitctx.channel.send(f"{ctx.user.display_name}did a thing.")EventsBy default, the following events are hooked by EventContext:Messagesmessagemessage_deletemessage_editraw_message_deleteraw_message_editReactionsreaction_addreaction_removeraw_reaction_addraw_reaction_removereaction_clearreaction_clear_emojiraw_reaction_clearraw_reaction_clear_emojiChannelstypingguild_channel_updateguild_channel_createguild_channel_deleteguild_channel_pins_updatewebhooks_updateGuildsguild_updateguild_joinguild_removeguild_integrations_updateguild_emojis_updateguild_availableguild_unavailableMembersmember_updatemember_joinmember_removemember_ban_hookmember_unban_hookRolesguild_role_update_hookguild_role_create_hookguild_role_delete_hookCommandscommandYou can add more event hooks or replace the default ones with the decorator:@EventContext.register_hook(event_name)defevent_hook(*args,**kwargs):...