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 common import tzlcmap, logPrint
# All command functions are expected to have this signature:
# def cmd_NAME(self, guild: discord.Guild, channel: discord.TextChannel, author: discord.User, msgcontent: str)
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
}
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.
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 = ' '
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
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] + "."
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
# 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):
em = discord.Embed(
@ -111,7 +113,7 @@ class WtCommands:
except KeyError:
await channel.send(':x: Not a valid zone name.')
return
resultstr = '```\n' + _tzPrint(zoneinput) + '\n```'
resultstr = '```\n' + self._tzPrint(zoneinput) + '\n```'
await channel.send(resultstr)
async def cmd_set(self, guild: discord.Guild, channel: discord.TextChannel, author: discord.User, msgcontent: str):
@ -151,7 +153,7 @@ class WtCommands:
return
resultarr = []
for i in clist:
resultarr.append(_tzPrint(i))
resultarr.append(self._tzPrint(i))
resultarr.sort()
resultstr = '```\n'
for i in resultarr:
@ -167,7 +169,7 @@ class WtCommands:
resultData = []
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()
resultFinal = '```\n'
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`.')
else: await channel.send(':x: The given user has not set a time zone. Ask to set it with `tz.set`.')
return
resultstr = '```\n' + _tzPrint(res[0]) + '\n```'
resultstr = '```\n' + self._tzPrint(res[0]) + '\n```'
await channel.send(resultstr)