Moved some utility functions to utility package

This commit is contained in:
Elnath 2020-12-29 01:31:23 +01:00
parent ccc438c89e
commit 34e8c1b94f
2 changed files with 28 additions and 21 deletions

View File

@ -10,6 +10,7 @@ import discord
import discord.utils import discord.utils
from ChannelsConfigFile import ChannelsConfigFile from ChannelsConfigFile import ChannelsConfigFile
import utils
logger = logging.getLogger("VocalMaisBot") logger = logging.getLogger("VocalMaisBot")
@ -52,17 +53,17 @@ class VocalMaisBot(discord.Client):
return return
contents = message.content.split() 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) 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:") 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) 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) 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) 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) return await self.clear_watched_channels(message)
else: else:
return await self.sorry_do_not_understand(message) 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) 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__': 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 = 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") argparser.add_argument("-t", "--token-file", default = ".token", help = "File where the discord bot token is stored")

View File

@ -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: 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 l[i] = v
# Remove the tail # Remove the tail
del l[i + 1: len(l)] 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