Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import discord
- import pytz, json, os
- from pytz import all_timezones
- from datetime import datetime, timedelta
- from dateutil.parser import parse
- import re
- # Create a new discord client
- client = discord.Client(intents=discord.Intents.all())
- # Dictionary to store users' timezones
- timezones = {}
- jsonDir = os.path.dirname(os.path.realpath(__file__))
- jsonPath = os.path.join(jsonDir,'timezones.json')
- # Load timezones from json file
- try :
- with open(jsonPath, 'r') as f:
- timezones = json.load(f)
- #convert the string keys to int
- timezones = {int(k): v for k, v in timezones.items()}
- except:
- pass
- @client.event
- async def on_message(message):
- if message.author.bot == False:
- # Check if message starts with "/settz" command
- if message.content.startswith('/settz'):
- # Get the timezone from the message
- tz = message.content[7:]
- # Check if the timezone is a valid one
- if tz in all_timezones:
- # Store the user's timezone in the dictionary
- authID = int(message.author.id)
- timezones[authID] = tz
- # Send confirmation message to user
- await message.channel.send(f'Timezone set to {tz}')
- # Save timezones to json file
- with open(jsonPath, 'w') as f:
- json.dump(timezones, f)
- # print(timezones)
- else:
- # Send error message if timezone is not valid
- await message.channel.send('Invalid timezone')
- # Check if message starts with "/gettz" command
- if message.content.startswith('/gettz'):
- # Retrieve user's timezone from dictionary
- # print(timezones)
- tz = timezones.get(message.author.id)
- if tz is None:
- # Send message if user's timezone is not set
- await message.channel.send('Timezone not set')
- else:
- # Send message with user's timezone
- await message.channel.send(f'Timezone is {tz}')
- # Check if message starts with "/time" command
- if message.content.startswith("/time"):
- input_time = message.content[6:]
- # Try to parse the input time using dateutil
- try:
- parsed_time = parse(input_time)
- tz = timezones.get(message.author.id)
- if tz is None:
- # Send message if user's timezone is not set
- await message.channel.send('Timezone not set')
- else:
- # Add timezone information to parsed time
- aware_time = pytz.timezone(tz).localize(parsed_time)
- parsed_time = parsed_time.replace(tzinfo=pytz.timezone(tz))
- # Get the Unix time
- unix_time = int(parsed_time.timestamp())
- dt_object = datetime.fromtimestamp(unix_time)
- # Define the time zones
- munich_tz = pytz.timezone("Europe/Prague")
- melbourne_tz = pytz.timezone("Australia/Melbourne")
- austin_tz = pytz.timezone("US/Central")
- detroit_tz = pytz.timezone("US/Eastern")
- los_angeles_tz = pytz.timezone("US/Pacific")
- # Convert the datetime object to the respective time zones
- munich_time = aware_time.astimezone(munich_tz)
- melbourne_time = aware_time.astimezone(melbourne_tz)
- austin_time = aware_time.astimezone(austin_tz)
- detroit_time = aware_time.astimezone(detroit_tz)
- los_angeles_time = aware_time.astimezone(los_angeles_tz)
- # Define the time format
- timeFormat = "%a %b %d %I:%M %p"
- # Send the converted times back to the channel
- await message.channel.send(f"`ATX: {austin_time.strftime(timeFormat)}`")
- await message.channel.send(f"`LAX: {los_angeles_time.strftime(timeFormat)}`")
- await message.channel.send(f"`MUN: {munich_time.strftime(timeFormat)}`")
- await message.channel.send(f"`MLB: {melbourne_time.strftime(timeFormat)}`")
- await message.channel.send(f"`MTL: {detroit_time.strftime(timeFormat)}`")
- # Send error message if input time is not valid
- except ValueError:
- await message.channel.send("Invalid input time. Please use a human-readable format like 'Jan 12 1pm'.\nNote that you have to use 2:30pm vs 230pm")
- return
- else:
- times = re.findall(r'\b(1[0-2]|0?[1-9]):?([0-5][0-9])? ?([ap]m)\b', message.content)
- datetime_obj = None
- for time in times:
- # await message.channel.send(time)
- if time[1] == '':
- timeString = f'{time[0]}00{time[2]}'
- else:
- timeString = f'{time[0]}{time[1]}{time[2]}'
- datetime_obj = datetime.strptime(timeString, "%I%M%p")
- formatted_time = datetime_obj.strftime("%I:%M %p")
- if formatted_time[:1] == '0':
- formatted_time = formatted_time[1:]
- # await message.channel.send(formatted_time)
- try:
- parsed_time = parse(formatted_time)
- tz = timezones.get(message.author.id)
- if tz is None:
- # Send message if user's timezone is not set
- await message.channel.send('Timezone not set')
- else:
- # Add timezone information to parsed time
- aware_time = pytz.timezone(tz).localize(parsed_time)
- parsed_time = parsed_time.replace(tzinfo=pytz.timezone(tz))
- # Get the Unix time
- unix_time = int(parsed_time.timestamp())
- dt_object = datetime.fromtimestamp(unix_time)
- # Define the time zones
- munich_tz = pytz.timezone("Europe/Prague")
- melbourne_tz = pytz.timezone("Australia/Melbourne")
- austin_tz = pytz.timezone("US/Central")
- detroit_tz = pytz.timezone("US/Eastern")
- los_angeles_tz = pytz.timezone("US/Pacific")
- # Convert the datetime object to the respective time zones
- munich_time = aware_time.astimezone(munich_tz)
- melbourne_time = aware_time.astimezone(melbourne_tz)
- austin_time = aware_time.astimezone(austin_tz)
- detroit_time = aware_time.astimezone(detroit_tz)
- los_angeles_time = aware_time.astimezone(los_angeles_tz)
- # Define the time format
- timeFormat = "%I:%M %p"
- # Send the converted times back to the channel
- await message.channel.send(f"`ATX: {austin_time.strftime(timeFormat)} | LAX: {los_angeles_time.strftime(timeFormat)} | MUN: {munich_time.strftime(timeFormat)} | MLB: {melbourne_time.strftime(timeFormat)} | MTL: {detroit_time.strftime(timeFormat)}`")
- # Send error message if input time is not valid
- except ValueError:
- await message.channel.send("Invalid input time. Please use a human-readable format like 'Jan 12 1pm'.\nNote that you have to use 2:30pm vs 230pm")
- return
- client.run('TOKEN')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement