Skip to content

Instantly share code, notes, and snippets.

@sprocter
Last active October 8, 2020 01:32
Show Gist options
  • Save sprocter/31b4fb653cea49f351686ff2265527c4 to your computer and use it in GitHub Desktop.
Save sprocter/31b4fb653cea49f351686ff2265527c4 to your computer and use it in GitHub Desktop.
A discord bot to convert times from one time zone into multiple others.
import discord
import logging
from dateutil.parser import parse
from dateutil.tz import gettz
from pytz import timezone
logging.basicConfig(level=logging.CRITICAL)
client = discord.Client()
tzinfos = {
"WPT": gettz("America/Chicago"), "CST": gettz("America/Chicago"), "CDT": gettz("America/Chicago"),
"JPT": gettz("America/Los Angeles"), "PST": gettz("America/Los Angeles"), "PDT": gettz("America/Los Angeles"),
"EST": gettz("America/New York"), "EDT": gettz("America/New York"), "EPT": gettz("America/New York"),
}
day_fmt = '%b %-d @ '
time_fmt = '%-I.%M%p'
@client.event
async def on_message(message):
# we do not want the bot to reply to itself
if message.author == client.user:
return
if message.content.startswith('!tz'):
try:
msg = get_converted_times(message.content[3:])
except Exception as e:
print(e)
msg = "Can't parse '" + message.content[3:] + "' sorry 🤖"
await message.channel.send(msg)
return
def get_converted_times(msg):
print(msg)
msg = msg.strip().replace('.',':',1)
#dt = add_default_tz(parse(msg.upper(), fuzzy=True, tzinfos=tzinfos), EST_tz)
dt = parse(msg.upper(), fuzzy_with_tokens=True, tzinfos=tzinfos)
print(dt)
pDt = dt[0].astimezone(timezone('US/Pacific')).strftime(time_fmt).lower() + " jpt"
cDt = dt[0].astimezone(timezone('US/Central')).strftime(time_fmt).lower() + " wpt"
eDt = dt[0].astimezone(timezone('US/Eastern')).strftime(time_fmt).lower() + " ept"
pDay = dt[0].astimezone(timezone('US/Pacific')).strftime(day_fmt)
cDay = dt[0].astimezone(timezone('US/Central')).strftime(day_fmt)
eDay = dt[0].astimezone(timezone('US/Eastern')).strftime(day_fmt)
if eDay == pDay:
return cDay + pDt + " -- " + cDt + " -- " + eDt
else:
return pDay + pDt + " -- " + cDay + cDt + " -- " + eDay + eDt
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
client.run('---Token Here---')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment