From 08230eb3dea6d893b625f1c1aa9b85f44f492830 Mon Sep 17 00:00:00 2001 From: Christoph Urlacher Date: Sat, 9 Dec 2023 18:44:16 +0100 Subject: [PATCH] Enforce heidi_spam channel for commands --- bot.py | 5 +++++ heidi_constants.py | 3 +++ heidi_helpers.py | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/bot.py b/bot.py index 3fa78ae..2a73d87 100644 --- a/bot.py +++ b/bot.py @@ -149,6 +149,7 @@ def user_entrance_sound_autocomplete( config_value="Der Wert, auf welche die Option gesetzt werden soll." ) @app_commands.autocomplete(config_value=user_config_value_autocomplete) +@enforce_channel(HEIDI_SPAM_ID) async def user_config( interaction: Interaction, config_key: str, config_value: str ) -> None: @@ -175,6 +176,7 @@ async def user_config( @client.tree.command(name="heidi", description="Heidi!") +@enforce_channel(HEIDI_SPAM_ID) async def heidi_exclaim(interaction: Interaction) -> None: """ Print a random Heidi quote. @@ -198,6 +200,7 @@ async def heidi_exclaim(interaction: Interaction) -> None: @client.tree.command(name="miesmuschel", description="Was denkt Heidi?") @app_commands.rename(question="frage") @app_commands.describe(question="Heidi wird es beantworten!") +@enforce_channel(HEIDI_SPAM_ID) async def magic_shell(interaction: Interaction, question: str) -> None: """ Answer a yes/no question. @@ -232,6 +235,7 @@ async def magic_shell(interaction: Interaction, question: str) -> None: @app_commands.describe(option_a="Ist es vielleicht dies?") @app_commands.rename(option_b="oder") @app_commands.describe(option_b="Oder doch eher das?") +@enforce_channel(HEIDI_SPAM_ID) async def choose(interaction: Interaction, option_a: str, option_b: str) -> None: """ Select an answer from two options. @@ -282,6 +286,7 @@ async def sound_autocomplete( @app_commands.describe(sound="Was soll Heidi sagen?") @app_commands.autocomplete(board=board_autocomplete) @app_commands.autocomplete(sound=sound_autocomplete) +@enforce_channel(HEIDI_SPAM_ID) async def say_voiceline(interaction: Interaction, board: str, sound: str) -> None: """ Play a voiceline in the calling member's current voice channel. diff --git a/heidi_constants.py b/heidi_constants.py index e3c2bfa..eb67ad7 100644 --- a/heidi_constants.py +++ b/heidi_constants.py @@ -24,3 +24,6 @@ SOUNDDIR: str = "/sounds" if DOCKER else "./heidi-sounds" # IDs of the servers Heidi is used on LINUS_GUILD = discord.Object(id=431154792308408340) TEST_GUILD = discord.Object(id=821511861178204161) + +# Channel IDs +HEIDI_SPAM_ID = 822223476101742682 \ No newline at end of file diff --git a/heidi_helpers.py b/heidi_helpers.py index 7a81f03..33f4462 100644 --- a/heidi_helpers.py +++ b/heidi_helpers.py @@ -1,4 +1,5 @@ import asyncio +import functools from typing import Union import discord @@ -9,6 +10,39 @@ from heidi_constants import * print("Debug: Importing heidi_helpers.py") +# Checks ----------------------------------------------------------------------------------------- + + +# 1. @enforce_channel(ID) is added to a function, which evaluates to decorate with the channel_id in its closure +# 2. The function is passed to decorate(function), +def enforce_channel(channel_id): + """ + Only run a function if called from the correct channel. + """ + def decorate(function): + + @functools.wraps(function) + async def wrapped(*args, **kwargs): + """ + Sends an interaction response if the interaction is not triggered from the heidi_spam channel. + """ + interaction: Interaction = args[0] + + # Do not call the decorated function if the channel_id doesn't match + if not interaction.channel_id == channel_id: + await interaction.response.send_message("Heidi sagt: Geh in heidi_spam du dulli") + return + + await function(*args, **kwargs) + + return wrapped + + return decorate + + +# Sounds ----------------------------------------------------------------------------------------- + + # @todo Normalize volume when playing async def play_voice_line( interaction: Union[Interaction, None],