Skip to content

Commit

Permalink
Implement bot parameter for AutocompleteContext and implement get_aut…
Browse files Browse the repository at this point in the history
…ocomplete_context
  • Loading branch information
CodeWithSwastik committed Nov 15, 2021
1 parent 3d16786 commit 16e7aff
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
36 changes: 35 additions & 1 deletion discord/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
UserCommand,
ApplicationCommand,
ApplicationContext,
AutocompleteContext,
command,
)
from .cog import CogMixin
Expand Down Expand Up @@ -431,7 +432,9 @@ async def process_application_commands(self, interaction: Interaction) -> None:
self.dispatch("unknown_command", interaction)
else:
if interaction.type is InteractionType.auto_complete:
return await command.invoke_autocomplete_callback(interaction)
ctx = await self.get_autocomplete_context(interaction)
ctx.command = command
return await command.invoke_autocomplete_callback(ctx)

ctx = await self.get_application_context(interaction)
ctx.command = command
Expand Down Expand Up @@ -567,6 +570,37 @@ class be provided, it must be similar enough to
cls = ApplicationContext
return cls(self, interaction)

async def get_autocomplete_context(
self, interaction: Interaction, cls=None
) -> AutocompleteContext:
r"""|coro|
Returns the autocomplete context from the interaction.
This is a more low-level counter-part for :meth:`.process_application_commands`
to allow users more fine grained control over the processing.
Parameters
-----------
interaction: :class:`discord.Interaction`
The interaction to get the invocation context from.
cls
The factory class that will be used to create the context.
By default, this is :class:`.AutocompleteContext`. Should a custom
class be provided, it must be similar enough to
:class:`.AutocompleteContext`\'s interface.
Returns
--------
:class:`.AutocompleteContext`
The autocomplete context. The type of this can change via the
``cls`` parameter.
"""
if cls is None:
cls = AutocompleteContext
return cls(self, interaction)



class BotBase(ApplicationCommandMixin, CogMixin):
_supports_prefixed_commands = False
Expand Down
20 changes: 11 additions & 9 deletions discord/commands/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,17 +527,19 @@ async def _invoke(self, ctx: ApplicationContext) -> None:
else:
await self.callback(ctx, **kwargs)

async def invoke_autocomplete_callback(self, interaction: Interaction):
async def invoke_autocomplete_callback(self, ctx: AutocompleteContext):
values = { i.name: i.default for i in self.options }

for op in interaction.data.get("options", []):
for op in ctx.interaction.data.get("options", []):
if op.get("focused", False):
option = find(lambda o: o.name == op["name"], self.options)
values.update({
i["name"]:i["value"]
for i in interaction.data["options"]
for i in ctx.interaction.data["options"]
})
ctx = AutocompleteContext(interaction, command=self, focused=option, value=op.get("value"), options=values)
ctx.focused = option
ctx.value = op.get("value")
ctx.options = values
if asyncio.iscoroutinefunction(option.autocomplete):
result = await option.autocomplete(ctx)
else:
Expand All @@ -547,7 +549,7 @@ async def invoke_autocomplete_callback(self, interaction: Interaction):
o if isinstance(o, OptionChoice) else OptionChoice(o)
for o in result
][:25]
return await interaction.response.send_autocomplete_result(choices=choices)
return await ctx.interaction.response.send_autocomplete_result(choices=choices)


def copy(self):
Expand Down Expand Up @@ -786,11 +788,11 @@ async def _invoke(self, ctx: ApplicationContext) -> None:
ctx.interaction.data = option
await command.invoke(ctx)

async def invoke_autocomplete_callback(self, interaction: Interaction) -> None:
option = interaction.data["options"][0]
async def invoke_autocomplete_callback(self, ctx: AutocompleteContext) -> None:
option = ctx.interaction.data["options"][0]
command = find(lambda x: x.name == option["name"], self.subcommands)
interaction.data = option
await command.invoke_autocomplete_callback(interaction)
ctx.interaction.data = option
await command.invoke_autocomplete_callback(ctx)


class ContextMenuCommand(ApplicationCommand):
Expand Down
16 changes: 10 additions & 6 deletions discord/commands/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ class AutocompleteContext:
Attributes
-----------
bot: :class:`.Bot`
The bot that the command belongs to.
interaction: :class:`.Interaction`
The interaction object that invoked the autocomplete.
command: :class:`.ApplicationCommand`
Expand All @@ -167,14 +169,16 @@ class AutocompleteContext:
A name to value mapping of the options that the user has selected before this option.
"""

__slots__ = ("interaction", "command", "focused", "value", "options")
__slots__ = ("bot", "interaction", "command", "focused", "value", "options")

def __init__(self, interaction: Interaction, *, command: ApplicationCommand, focused: Option, value: str, options: dict) -> None:
def __init__(self, bot: Bot, interaction: Interaction) -> None:
self.bot = bot
self.interaction = interaction
self.command = command
self.focused = focused
self.value = value
self.options = options

# self.command = command
# self.focused = focused
# self.value = value
# self.options = options

@property
def cog(self) -> Optional[Cog]:
Expand Down

0 comments on commit 16e7aff

Please sign in to comment.