From 34e8c1b94f55215394fdadbf337689792258346d Mon Sep 17 00:00:00 2001 From: Elnath Date: Tue, 29 Dec 2020 01:31:23 +0100 Subject: [PATCH] Moved some utility functions to utility package --- VocalMaisBot.py | 27 +++++++-------------------- utils/__init__.py | 22 +++++++++++++++++++++- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/VocalMaisBot.py b/VocalMaisBot.py index 1ca983a..cc7f2e5 100755 --- a/VocalMaisBot.py +++ b/VocalMaisBot.py @@ -10,6 +10,7 @@ import discord import discord.utils from ChannelsConfigFile import ChannelsConfigFile +import utils logger = logging.getLogger("VocalMaisBot") @@ -52,17 +53,17 @@ class VocalMaisBot(discord.Client): return contents = message.content.split() - if _check_list_element(contents, 1, "help"): + if utils.check_list_element_no_bounds(contents, 1, "help"): return await self.print_help(message.channel) - elif _check_list_element(contents, 1, "ping"): + elif utils.check_list_element_no_bounds(contents, 1, "ping"): return await message.channel.send(":ping_pong:") - elif _check_list_element(contents, 1, "register"): + elif utils.check_list_element_no_bounds(contents, 1, "register"): return await self.register_channel(message) - elif _check_list_element(contents, 1, "forget"): + elif utils.check_list_element_no_bounds(contents, 1, "forget"): return await self.forget_channel(message) - elif _check_list_element(contents, 1, "list"): + elif utils.check_list_element_no_bounds(contents, 1, "list"): return await self.list_watched_channels(message) - elif _check_list_element(contents, 1, "clear"): + elif utils.check_list_element_no_bounds(contents, 1, "clear"): return await self.clear_watched_channels(message) else: return await self.sorry_do_not_understand(message) @@ -196,20 +197,6 @@ class VocalMaisBot(discord.Client): self.channels_config.remove_channel_from_created(channel.guild, channel) -def _check_list_element(l: List, index: int, expected_value: Any) -> bool: - try: - return l[index] == expected_value - except IndexError: - return False - - -def _check_dict_element(d: Dict, key: Any, expected_value: Any) -> bool: - try: - return d[key] == expected_value - except KeyError: - return False - - if __name__ == '__main__': argparser = argparse.ArgumentParser(description = "Discord bot to automatically create temporary voice channels for users when they connect to a special channel", formatter_class = argparse.ArgumentDefaultsHelpFormatter) argparser.add_argument("-t", "--token-file", default = ".token", help = "File where the discord bot token is stored") diff --git a/utils/__init__.py b/utils/__init__.py index d3b31da..c312e8c 100644 --- a/utils/__init__.py +++ b/utils/__init__.py @@ -1,4 +1,4 @@ -from typing import List, Any +from typing import List, Dict, Any def list_remove_all_occurrences(l: List[Any], value: Any) -> None: @@ -13,3 +13,23 @@ def list_remove_all_occurrences(l: List[Any], value: Any) -> None: l[i] = v # Remove the tail del l[i + 1: len(l)] + + +def check_list_element_no_bounds(l: List[Any], index: int, expected_value: Any) -> bool: + """ + Check if l[index] is equal to expected_value, but without raising an exception if l does not have that many elements + """ + try: + return l[index] == expected_value + except IndexError: + return False + + +def check_dict_element_no_exception(d: Dict[Any, Any], key: Any, expected_value: Any) -> bool: + """ + Check if d[key] is equal to expected_value, but without raising an exception if d does not contain the key + """ + try: + return d[key] == expected_value + except KeyError: + return False