Skip to content

Commit 3616516

Browse files
committed
Add autocomplete to app commands
1 parent a7d7d05 commit 3616516

File tree

7 files changed

+44
-6
lines changed

7 files changed

+44
-6
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ __pycache__/
44

55
config.toml
66
bashbot.log
7-
state.json
7+
state.json
8+
build.json

bashbot/command/controls.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import discord
2-
from discord import Message, Interaction, Embed
2+
from discord import Message, Interaction, Embed, app_commands
33
from discord.ext import commands
44
from discord.ext.commands import Context
55
from discord.ui import View
@@ -77,3 +77,12 @@ async def remove(self, ctx: Context, label: str = None):
7777
if ctx.interaction:
7878
embed = Embed(description=f"Control removed", color=0xff0000)
7979
await ctx.reply(embed=embed, ephemeral=False, delete_after=0)
80+
81+
@remove.autocomplete('label')
82+
async def remove_autocomplete(self, interaction: Interaction, current: str):
83+
terminal: Terminal = sessions().by_channel(interaction.channel)
84+
results = terminal.search_control(current)
85+
return [
86+
app_commands.Choice(name=option, value=option)
87+
for option in results
88+
]

bashbot/command/macro.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
from discord import Embed
1+
from discord import Embed, Interaction, app_commands
22
from discord.ext import commands
33
from discord.ext.commands import Context
44

55
from bashbot.command import session_exists
66
from bashbot.constants import EMBED_COLOR
7-
from bashbot.core.macros import execute_macro
7+
from bashbot.core.macros import execute_macro, search_macro
88

99

1010
class MacroCommand(commands.Cog):
11-
@commands.command(
11+
@commands.hybrid_command(
1212
name='macro',
1313
aliases=['.macro', '.m'],
1414
description='Executes macro from "macros" directory',
@@ -20,3 +20,11 @@ async def macro(self, ctx: Context, macro_name: str):
2020

2121
embed = Embed(description=f"Executed macro {macro_name}", color=EMBED_COLOR)
2222
await ctx.send(embed=embed)
23+
24+
@macro.autocomplete('macro_name')
25+
async def macro_autocomplete(self, interaction: Interaction, current: str):
26+
results = search_macro(current)
27+
return [
28+
app_commands.Choice(name=option, value=option)
29+
for option in results
30+
]

bashbot/command/select.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from discord import Embed
1+
from discord import Embed, Interaction, app_commands
22
from discord.ext import commands
33

44
from bashbot.constants import EMBED_COLOR
@@ -21,3 +21,11 @@ async def select(self, ctx, name):
2121
sessions().select(ctx.message.channel, terminal)
2222
embed = Embed(description=f"Selected terminal #{name}", color=EMBED_COLOR)
2323
await ctx.send(embed=embed)
24+
25+
@select.autocomplete('name')
26+
async def select_autocomplete(self, interaction: Interaction, current: str):
27+
results = sessions().search(current)
28+
return [
29+
app_commands.Choice(name=option.name, value=option.name)
30+
for option in results
31+
]

bashbot/core/macros.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ async def execute_macro(ctx, name):
1717
await ctx.bot.process_commands(ctx.message)
1818
else:
1919
terminal.send_input(line)
20+
21+
22+
def search_macro(phrase):
23+
return [name for name in settings().macros.keys() if name.startswith(phrase)]

bashbot/terminal/sessions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import List
2+
13
from discord import TextChannel, Message
24

35
from bashbot.core.factory import SingletonDecorator
@@ -27,6 +29,9 @@ def by_message(self, searched_message: Message) -> Terminal:
2729
if searched_message.id == message.id:
2830
return terminal
2931

32+
def search(self, phrase: str) -> List[Terminal]:
33+
return [terminal for terminal in self.sessions.values() if terminal.name.startswith(phrase)]
34+
3035
def by_name(self, name: str) -> Terminal:
3136
for message, terminal in self.sessions.items():
3237
if terminal.name == name:

bashbot/terminal/terminal.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ def add_control(self, emoji, content):
8383
def remove_control(self, emoji):
8484
self.controls.pop(emoji)
8585

86+
def search_control(self, phrase):
87+
return [label for label in self.controls.keys() if label.startswith(phrase)]
88+
8689
def __monitor_pty(self, loop):
8790
try:
8891
output = os.read(self.fd, 1024)

0 commit comments

Comments
 (0)