add sound autocomplete

This commit is contained in:
2022-10-28 17:19:28 +02:00
parent fea885c4d1
commit efb9e4869b

15
bot.py
View File

@ -2,6 +2,7 @@
import os, re, random, logging, asyncio, discord
from discord import app_commands
from discord.app_commands import Choice
from functools import reduce
from dotenv import load_dotenv
from typing import Optional
@ -177,16 +178,15 @@ async def show_link(interaction: discord.Interaction):
SOUNDDIR: str = "./voicelines/" # Local
# SOUNDDIR: str = "/sounds/" # Docker
# Example: https://discordpy.readthedocs.io/en/latest/interactions/api.html?highlight=autocomplete#discord.app_commands.autocomplete
async def sound_autocomplete(interaction: discord.Interaction, current: str) -> list[Choice[str]]:
sounds = map(lambda x: x.split(".")[0], os.listdir(SOUNDDIR))
return [Choice(name=sound, value=sound) for sound in sounds]
@client.tree.command(name = "sag", description = "Heidi drückt den Knopf auf dem Soundboard.")
@app_commands.describe(sound = "Was soll Heidi sagen?")
@app_commands.autocomplete(sound = sound_autocomplete)
async def say_voiceline(interaction: discord.Interaction, sound: str):
voicelines = map(lambda x: x.split(".")[0], os.listdir(SOUNDDIR)) # only works from docker
if not sound in voicelines:
print("Invalid sound!")
await interaction.response.send_message(f"Heidi sagt: \"{sound}\" kanninich finden bruder")
return
# Only Members can access voice channels
if not isinstance(interaction.user, discord.Member):
print("User not a member")
@ -221,7 +221,6 @@ async def say_voiceline(interaction: discord.Interaction, sound: str):
await voice_client.disconnect()
# Example
# Callable on members
@client.tree.context_menu(name='Show Join Date')