-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheventhandler.py
More file actions
237 lines (168 loc) · 8.99 KB
/
eventhandler.py
File metadata and controls
237 lines (168 loc) · 8.99 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import discord, aiohttp, json
from math import ceil
from champid import champ_id
from config import token_dict, region_dict
from summonersearch import summoner_search
"""
Comments about program:
Below is the code for the discord bot and all the commands it can take.
Originally I wanted to split everything up into methods but I will do that
once I understand the decorators for the discord module
"""
client = discord.Client()
@client.event # Event for when bot is ready
async def on_ready():
print("LoL Info Tool is ready!")
@client.event
async def on_message(msg):
# Welcome Message: Complete
if msg.content.lower().startswith('!hello'):
channel = msg.channel
await channel.send('Hello there summoner, I am ready to provide support. For a list of inputs, type: !help')
# Help Message: Work in progress
elif msg.content.lower().startswith('!help'):
""" The following are the help messages
!help: Menu of commands and inputs
!hello: Welcome message
!summoner <Summoner Name>: Returns info on given summoner, summoner name must not contain punctuation or accents
!region <Region>: Changes the region the bot operates in. Changing the region affects everyone else using the bot in the server
"""
channel = msg.channel
help_msg = '!help: Menu of commands and inputs\n' \
'!hello: Welcome message\n' \
'!summoner <Summoner Name>: Returns info on given ' \
'summoner, summoner name must not contain punctuation ' \
'or accents (GOOD: BestMidNA, Nuclear Gandhi, etc. BAD: ' \
'IEat.Dimsum, Flip,And Dip, etc.)\n' \
'!region <Region>: Changes the region the bot operates ' \
'in. Changing the region affects everyone else using ' \
'the bot in the server (Region Codes: BR, EUNE, EUW, JP, ' \
'KR, LAN, LAS, NA, OCE, TR, RU, PBE) (NA is default)'
embed = discord.Embed(title='Help',
description='Menu of Commands and Inputs',
color=0x0000ff)
embed.add_field(name='Commands', value=help_msg, inline=False)
embed.set_footer(text='Page 1/1')
await channel.send(embed=embed)
# Change Region: Complete
elif msg.content.lower().startswith('!region'):
channel = msg.channel
new_code = msg.content.upper()[8:].strip(' ')
if new_code in region_dict.keys():
if new_code == region_dict['current']:
await channel.send('The tool is already set on ' + new_code)
else:
region_dict['current'] = new_code
await channel.send('The tool is now set to ' +
region_dict['current'] + ' on the url: '
+ region_dict[
region_dict['current']].lower()
+ '.api.riotgames.com')
else:
await channel.send('The region code ' + new_code +
' does not exist')
# Summoner Search: Work in progress
elif msg.content.lower().startswith('!summoner'):
'''Returns an embedded message with the summoner's name, summoner
id, level, icon, and top three champions by mastery
'''
channel = msg.channel
valid_name = True
name = msg.content[10:].strip()
await channel.send('Received summoner name ' + name + ' from user '
+ str(msg.author))
punc = ',./;\'[]<>?:\"{}!@#$%^&*()_+-='
for item in punc:
if item in name:
valid_name = False
break
if valid_name and len(name) >= 4: # Implement once module is complete
summoner_dict = await summoner_search(name)
if 'id' not in summoner_dict.keys():
await channel.send('The summoner name ' + name + \
' cannot be found. Names with special '
'characters cannot be searched, '
'otherwise try switching regions.')
else:
await channel.send('Sending info on summoner: ' + name )
champ_masteries = summoner_dict['champ_mastery']
if len(champ_masteries) < 5:
await channel.send('The summoner you searched has less \
than five champions played, so only their most played \
champions will be displayed')
champ = {} # For exceptions
mastery_msg = ''
try:
for item in champ_masteries:
champ = item
mastery_msg += champ_id[item['championId']]
mastery_msg += ' [lvl ' + str(item['championLevel']) + ']'
mastery_msg += ': ' + str(item['championPoints']) + '\n'
except KeyError:
await channel.send('Missing a champion id reference,'
' the missing champion\'s id is ' +
champ['championID'])
#Ranked info
ranked_list = summoner_dict['ranked']
if ranked_list == []:
ranked_msg = 'Unranked'
else:
counter = -1
for i in range(len(ranked_list)):
if ranked_list[i]['queueType'] == 'RANKED_SOLO_5x5':
counter = i
if counter == -1:
ranked_msg = 'Unranked'
else:
ranked_msg = ranked_list[counter]['tier'] + ' ' + \
ranked_list[counter]['rank']
# Time info
if summoner_dict['time'] == -1:
time_msg = '0'
else:
time_msg = '~' + str(summoner_dict['time']) + ' minutes'
hours_spent = ceil(int(summoner_dict['time'])/60)
time_msg += '/~' + str(hours_spent) + ' hours'
days_spent = round((int(summoner_dict['time'])/60)/24, 1)
time_msg += '/~' + str(days_spent) + ' days'
# Time per week info
if summoner_dict['time_week'] == 0:
time_per_week_msg = '0'
else:
if summoner_dict['time_week'] > 3600:
hours_this_week = summoner_dict['time_week']//3600
minutes_this_week = summoner_dict['time_week']\
%3600//60
else:
hours_this_week = 0
minutes_this_week = summoner_dict['time_week']//60
time_per_week_msg = str(hours_this_week) + ' hours and '\
+ str(minutes_this_week) + \
' minutes in the past 7 days'
embed = discord.Embed(title='Summoner: ' + name,
description= name + '\'s profile',
color=0x0000ff)
embed.set_thumbnail(url='http://avatar.leagueoflegends.com/'
+ region_dict['current'].lower() +
'/' + name.replace(' ','+') + '.png')
embed.add_field(name='Level', value= summoner_dict
['summonerLevel'], inline=True)
embed.add_field(name='Region', value=region_dict['current'],
inline=True)
embed.add_field(name='Rank', value=ranked_msg,
inline=True)
embed.add_field(name='Champion Mastery', value=mastery_msg,
inline=True)
embed.add_field(name='Time Spent', value=time_msg,
inline=True)
embed.add_field(name='Time Spent (Past 7 Days)',
value= time_per_week_msg, inline=True)
embed.add_field(name='More Details',
value='https://na.op.gg/summoner/userName='
+ name.replace(' ','+'), inline=True)
embed.set_footer(text='LoLInfoTool is not associated with'
' op.gg or wol.gg')
await channel.send(embed=embed)
else:
await channel.send('The summoner name ' + name + ' is invalid')
client.run(token_dict['discord'])