-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
131 lines (112 loc) · 5.03 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import datetime
import pytz
import discord
from dateutil import tz
from discord.ext import commands
description = '''A bot that both provides current time, and future time conversions.'''
bot = commands.Bot(command_prefix='?', description=description)
@bot.event
async def on_ready():
print('Logged in as')
print(bot.user.name)
print(bot.user.id)
print('------')
@bot.command(pass_context=True)
async def time(ctx):
"""Shows the time in a range of timezones."""
async with ctx.typing():
timezones_to_convert_dict = {
'PT/California': 'America/Los_Angeles',
'MT/Alberta': 'America/Edmonton',
'SK': 'Canada/Saskatchewan',
'CT/Winnipeg': 'America/Winnipeg',
'ET/New York': 'America/New_York',
'UK/London': 'Europe/London',
'CET/Copenhagen': 'Europe/Copenhagen',
'AEST/Sydney': 'Australia/Sydney',
}
tz_field = []
time_field = []
for display, zone in timezones_to_convert_dict.items():
tz_field.append("**{}**".format(display))
time_field.append(datetime.datetime.now(pytz.timezone(zone)).strftime('%H:%M'))
embed = discord.Embed(title="Time Conversions", description='Current Times', color=0x00ff00)
embed.add_field(name="Time Zones", value='\n'.join(tz_field), inline=True)
embed.add_field(name="Time", value='\n'.join(time_field), inline=True)
await ctx.send(embed=embed)
file = open('log.txt', 'a+')
file.write(
'Server: {} Time: {} Command: {} \n'.format(discord.Message.guild,
datetime.datetime.now().strftime('%H:%M %d/%m/%y'), 'convert'))
file.close()
@bot.command(pass_context=True)
async def convert(ctx, time_zone_input: str, *time_input: str):
"""Shows a range of timezones in the future. Format: h:m day/month/year. Example 12:30 4/8/2018"""
async with ctx.typing():
try:
time_join = " ".join(time_input)
time_join.strip(' ')
time_to_convert = datetime.datetime.strptime(str(time_join), '%H:%M %d/%m/%y')
tz_dict = {
'PT': 'America/Los_Angeles',
'MT': 'America/Edmonton',
'SK': 'Canada/Saskatchewan',
'CT': 'America/Winnipeg',
'ET': 'America/New_York',
'UK': 'Europe/London',
'CET': 'Europe/Paris',
'AU': 'Australia/Sydney'
}
timezones_to_use = {
'PT/California': 'America/Los_Angeles',
'MT/Alberta': 'America/Edmonton',
'SK/Saskatchewan': 'Canada/Saskatchewan',
'CT/Winnipeg': 'America/Winnipeg',
'ET/New York': 'America/New_York',
'UK/London': 'Europe/London',
'CET/Copenhagen': 'Europe/Copenhagen',
'AEST/Sydney': 'Australia/Sydney',
}
from_zone = tz.gettz(tz_dict.get(time_zone_input))
tz_field = []
time_field = []
for display, zone in timezones_to_use.items():
tz_field.append("**{}**".format(display))
mt = time_to_convert.replace(tzinfo=from_zone)
to_zone = tz.gettz(zone)
converted = mt.astimezone(to_zone)
time_field.append(converted.strftime('%H:%M %d/%m/%y'))
embed = discord.Embed(title="Time Conversions", description='In the Future', color=0x00ff00)
embed.add_field(name="Time Zones", value='\n'.join(tz_field), inline=True)
embed.add_field(name="Time", value='\n'.join(time_field), inline=True)
await ctx.send(embed=embed)
except ValueError:
await ctx.send('Check formatting for your command. Must be in HH:MM d/m/y using the 24 hour clock.')
file = open('log.txt', 'a+')
file.write(
'Server: {} Time: {} Command: {} \n'.format(discord.Member.guild,
datetime.datetime.now().strftime('%H:%M %d/%m/%y'), 'convert'))
file.close()
@bot.command(pass_context=True)
async def timezones(ctx):
"""Shows the timezones able to be used in the convert command."""
tz_dict = {
'PT': 'Pacific Time',
'MT': 'Mountain Time',
'SK': 'Saskatchewan Time',
'CT': 'Central Time',
'ET': 'Eastern Time',
'UK': 'United Kingdom',
'CET': 'Central European Time',
'AU': 'Australian (Sydney)'
}
embed = discord.Embed(title='Time Zones', description='Usable Timezones', color=0x00ff00)
embed.add_field(name='Time Zones', value='\n'.join(tz_dict), inline=True)
embed.add_field(name='Names', value='\n'.join(tz_dict.values()))
await ctx.send(embed=embed)
file = open('log.txt', 'a+')
file.write(
'Server: {} Time: {} Command: {} \n'.format(discord.Member.guild,
datetime.datetime.now().strftime('%H:%M %d/%m/%y'), 'timezones'))
file.close()
bot.run('INSERT TOKEN HERE')