Moved helper methods into class

This commit is contained in:
Noikoio 2018-09-01 12:41:44 -07:00
parent bf80c32402
commit 4bc7836ec0

View file

@ -10,10 +10,30 @@ from datetime import datetime
from userdb import UserDatabase from userdb import UserDatabase
from common import tzlcmap, logPrint from common import tzlcmap, logPrint
# All command functions are expected to have this signature: class WtCommands:
# def cmd_NAME(self, guild: discord.Guild, channel: discord.TextChannel, author: discord.User, msgcontent: str) def __init__(self, userdb: UserDatabase, client: discord.Client):
self.userdb = userdb
self.dclient = client
self.commandlist = {
'help' : self.cmd_help,
'list' : self.cmd_list,
'time' : self.cmd_time,
'set' : self.cmd_set,
'remove': self.cmd_remove
}
def _tzPrint(zone : str): async def dispatch(self, cmdBase: str, message: discord.Message):
try:
command = self.commandlist[cmdBase]
except KeyError:
return
logPrint('Command invoked', '{0}/{1}: tz.{2}'.format(message.guild, message.author, cmdBase))
await command(message.guild, message.channel, message.author, message.content)
# ------
# Helper methods
def _tzPrint(self, zone : str):
""" """
Returns a string displaying the current time in the given time zone. Returns a string displaying the current time in the given time zone.
Resulting string should be placed in a code block. Resulting string should be placed in a code block.
@ -23,7 +43,7 @@ def _tzPrint(zone : str):
if len(now_time.strftime("%Z")) != 4: padding = ' ' if len(now_time.strftime("%Z")) != 4: padding = ' '
return "{:s}{:s} | {:s}".format(now_time.strftime("%H:%M %d-%b %Z%z"), padding, zone) return "{:s}{:s} | {:s}".format(now_time.strftime("%H:%M %d-%b %Z%z"), padding, zone)
def _userResolve(guild: discord.Guild, userIds: list): def _userResolve(self, guild: discord.Guild, userIds: list):
""" """
Given a list with user IDs, returns a string, the second half of a Given a list with user IDs, returns a string, the second half of a
list entry, describing the users for which a zone is represented by. list entry, describing the users for which a zone is represented by.
@ -57,28 +77,10 @@ def _userResolve(guild: discord.Guild, userIds: list):
result = result[:-2] + "." result = result[:-2] + "."
return result return result
class WtCommands:
def __init__(self, userdb: UserDatabase, client: discord.Client):
self.userdb = userdb
self.dclient = client
self.commandlist = {
'help' : self.cmd_help,
'list' : self.cmd_list,
'time' : self.cmd_time,
'set' : self.cmd_set,
'remove': self.cmd_remove
}
async def dispatch(self, cmdBase: str, message: discord.Message):
try:
command = self.commandlist[cmdBase]
except KeyError:
return
logPrint('Command invoked', '{0}/{1}: tz.{2}'.format(message.guild, message.author, cmdBase))
await command(message.guild, message.channel, message.author, message.content)
# ------ # ------
# Individual command handlers # Individual command handlers
# All command functions are expected to have this signature:
# def cmd_NAME(self, guild: discord.Guild, channel: discord.TextChannel, author: discord.User, msgcontent: str)
async def cmd_help(self, guild: discord.Guild, channel: discord.TextChannel, author: discord.User, msgcontent: str): async def cmd_help(self, guild: discord.Guild, channel: discord.TextChannel, author: discord.User, msgcontent: str):
em = discord.Embed( em = discord.Embed(
@ -111,7 +113,7 @@ class WtCommands:
except KeyError: except KeyError:
await channel.send(':x: Not a valid zone name.') await channel.send(':x: Not a valid zone name.')
return return
resultstr = '```\n' + _tzPrint(zoneinput) + '\n```' resultstr = '```\n' + self._tzPrint(zoneinput) + '\n```'
await channel.send(resultstr) await channel.send(resultstr)
async def cmd_set(self, guild: discord.Guild, channel: discord.TextChannel, author: discord.User, msgcontent: str): async def cmd_set(self, guild: discord.Guild, channel: discord.TextChannel, author: discord.User, msgcontent: str):
@ -151,7 +153,7 @@ class WtCommands:
return return
resultarr = [] resultarr = []
for i in clist: for i in clist:
resultarr.append(_tzPrint(i)) resultarr.append(self._tzPrint(i))
resultarr.sort() resultarr.sort()
resultstr = '```\n' resultstr = '```\n'
for i in resultarr: for i in resultarr:
@ -167,7 +169,7 @@ class WtCommands:
resultData = [] resultData = []
for key, value in rawlist.items(): for key, value in rawlist.items():
resultData.append(_tzPrint(key) + '\n' + _userResolve(guild, value)) resultData.append(self._tzPrint(key) + '\n' + self._userResolve(guild, value))
resultData.sort() resultData.sort()
resultFinal = '```\n' resultFinal = '```\n'
for i in resultData: for i in resultData:
@ -192,5 +194,5 @@ class WtCommands:
if spaghetti: await channel.send(':x: You do not have a time zone. Set it with `tz.set`.') if spaghetti: await channel.send(':x: You do not have a time zone. Set it with `tz.set`.')
else: await channel.send(':x: The given user has not set a time zone. Ask to set it with `tz.set`.') else: await channel.send(':x: The given user has not set a time zone. Ask to set it with `tz.set`.')
return return
resultstr = '```\n' + _tzPrint(res[0]) + '\n```' resultstr = '```\n' + self._tzPrint(res[0]) + '\n```'
await channel.send(resultstr) await channel.send(resultstr)