-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
333 lines (276 loc) · 16.7 KB
/
main.py
File metadata and controls
333 lines (276 loc) · 16.7 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import aiohttp
import discord
import asyncio
import sys
import time as t
from dotenv import load_dotenv
from teamBuild import checkID, teambuild
from commandInfo import commandInfo
from ingameAnalysis import get_summoner_id, get_puuid, get_current_game_info, get_strategy
from meta import get_latest_meta
from searchSummoner import search
# Ubuntu 서버에 저장된 환경변수 값 가져오기
import os
# .env 파일에서 환경변수를 로드할 경우 주석 제거
# load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
# print(TOKEN)
if sys.platform == 'win32':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
# intents 설정은 꼭 해줘야 한다!
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
intents.guilds = True
client = discord.Client(intents=intents)
@client.event
async def on_ready():
await client.change_presence(status=discord.Status.online, activity=discord.Game('분석'))
print(f'{client.user}이 준비 과정을 성공적으로 마쳤습니다.')
def find_first_channel(channels):
position_array = [i.position for i in channels]
for i in channels:
if i.position == min(position_array):
return i
@client.event
async def on_guild_join(guild):
for channel in guild.text_channels:
if channel.permissions_for(guild.me).send_messages:
await guild.system_channel.send("\"" + guild.name + "\"" + " 서버 여러분 안녕하세요!")
await guild.system_channel.send("저는 여러분이 롤을 편하게 즐길 수 있게 도와드리는 P.S Bot입니다.")
await guild.system_channel.send("저를 사용하는 방법에 대해선 아래를 참고해주세요!")
await guild.system_channel.send(embed=commandInfo())
break
@client.event
async def on_member_join(member):
await find_first_channel(member.guild.text_channels).send(member.name + "님 안녕하세요, 저는 P.S Bot입니다.")
await find_first_channel(member.guild.text_channels).send("도움이 필요하시면 \"!도움\" 을 외쳐주세요!")
@client.event
async def on_message(msg):
if msg.author == client.user:
return None
if msg.content == ('!도움'):
await msg.delete()
await msg.channel.send(embed=commandInfo())
if msg.content == ('!메타정보'):
temp_message = await msg.channel.send("자료 불러오는 중 ...") # 임시 메시지 저장
embed = await get_latest_meta()
await temp_message.delete() # 임시 메시지 삭제
if embed:
t1 = t.time()
await msg.channel.send(embed=embed)
await msg.delete()
t2 = t.time()
embed = discord.Embed(title="데이터 출처", description="[Youtube] 프로관전러 P.S", color=0x62c1cc)
embed.add_field(name="소요시간", value="`" + str(round(t2 - t1, 3)) + "초`", inline=False)
embed.set_footer(text="프로관전러 P.S 유튜브 자료를 가져왔습니다.",
icon_url="https://i.ibb.co/4f1nw7T/P-S.webp?type=w800")
await msg.channel.send(embed=embed)
else:
await msg.delete()
await msg.channel.send("죄송합니다. 아직 현재 메타 정보가 준비되지 않았습니다.")
if msg.content.startswith('!전적검색 '):
parts = msg.content.split('#', 1)
if len(parts) != 2:
embed = discord.Embed(title="명령어 형식이 잘못되었습니다.",
description="`!전적검색 닉네임#태그` 형식으로 입력해주세요.", color=0x62c1cc)
embed.set_thumbnail(url="https://i.ibb.co/4f1nw7T/P-S.webp?type=w800")
await msg.channel.send(embed=embed)
return
summoner_name = parts[0].replace('!전적검색 ', '')
summoner_tag = parts[1]
async with aiohttp.ClientSession() as session:
temp_message = await msg.channel.send("전적 검색 중 ...") # 임시 메시지 저장
file = await search(session, summoner_name, summoner_tag)
if file == -1:
embed = discord.Embed(title="해당 유저가 존재하지 않습니다.",
description="닉네임을 다시 확인해주세요! (띄어쓰기, 영어 대소문자)", color=0x62c1cc)
embed.set_thumbnail(url="https://i.ibb.co/4f1nw7T/P-S.webp?type=w800")
else:
await msg.delete()
t1 = t.time()
await msg.channel.send(file=file)
await temp_message.delete() # 임시 메시지 삭제
t2 = t.time()
embed = discord.Embed(title="데이터 출처", description="Riot API / fow.kr / 나무위키", color=0x62c1cc)
embed.add_field(name="소요시간", value="`" + str(round(t2 - t1, 3)) + "초`", inline=False)
embed.set_footer(text="솔로 랭크 기준 티어입니다.", icon_url="https://i.ibb.co/4f1nw7T/P-S.webp?type=w800")
await msg.channel.send(embed=embed)
if msg.content.startswith('!인게임분석 '):
parts = msg.content.split('#', 1)
if len(parts) != 2:
embed = discord.Embed(title="명령어 형식이 잘못되었습니다.",
description="`!인게임분석 닉네임#태그` 형식으로 입력해주세요.", color=0x62c1cc)
embed.set_thumbnail(url="https://i.ibb.co/4f1nw7T/P-S.webp?type=w800")
await msg.channel.send(embed=embed)
return
summoner_name = parts[0].replace('!인게임분석 ', '')
summoner_tag = parts[1]
async with aiohttp.ClientSession() as session:
summoner_puuid = await get_puuid(session, summoner_name, summoner_tag)
if summoner_puuid == -1:
await msg.delete()
embed = discord.Embed(title="해당 유저가 존재하지 않습니다.",
description="정확한 닉네임이 맞는지 확인해주세요! (띄어쓰기, 영어 대/소문자)", color=0x62c1cc)
embed.set_thumbnail(url="https://i.ibb.co/4f1nw7T/P-S.webp?type=w800")
await msg.channel.send(embed=embed)
else:
summoner_id = await get_summoner_id(session, summoner_puuid)
current_game = await get_current_game_info(session, summoner_id)
if current_game == -1:
await msg.delete()
embed = discord.Embed(title="현재 게임 중이 아닙니다.",
description="게임이 끝났나봐요! 아니면.. 혹시 닷지하셨나요?", color=0x62c1cc)
embed.set_thumbnail(url="https://i.ibb.co/4f1nw7T/P-S.webp?type=w800")
await msg.channel.send(embed=embed)
else:
temp_message = await msg.channel.send("인게임 분석 중 ...") # 임시 메시지 저장
t1 = t.time()
embed = await get_strategy(session, summoner_name, summoner_tag, summoner_puuid, current_game)
await msg.delete()
await msg.channel.send(embed=embed)
await temp_message.delete() # 임시 메시지 삭제
t2 = t.time()
embed = discord.Embed(title="데이터 출처", description="Riot API", color=0x62c1cc)
embed.add_field(name="소요시간", value="`" + str(round(t2 - t1, 3)) + "초`", inline=False)
embed.set_footer(text="게임 내의 모든 플레이어들의 승률 지표를 기반으로 나타난 결과입니다.",
icon_url="https://i.ibb.co/4f1nw7T/P-S.webp?type=w800")
await msg.channel.send(embed=embed)
if msg.content == '!내전팀빌딩':
async with aiohttp.ClientSession() as session:
await msg.delete()
temp_message = await msg.channel.send("내전에 참여할 10명의 유저의 닉네임과 태그를 '중복되지 않도록' 3분 이내에 입력해주세요."
"\n`ex.닉네임1#태그1/닉네임2#태그/...`")
def check(m):
return m.author == msg.author and m.channel == msg.channel
try:
response = await client.wait_for('message', timeout=180, check=check)
await response.delete()
except asyncio.TimeoutError:
embed = discord.Embed(title="시간 초과입니다 ㅜㅜ ",
description="저를 기다리고 계신 분들이 계시니.. 나중에 다시 시도해주세요!", color=0x62c1cc)
embed.set_thumbnail(url="https://i.ibb.co/4f1nw7T/P-S.webp?type=w800")
await msg.channel.send(embed=embed)
return
players = response.content.split('/')
if len(players) != 10:
embed = discord.Embed(title="10명의 유저를 입력하시지 않았거나 잘못된 형식으로 입력하신 것 같아요.",
description="`ex) 닉네임1#태그1/닉네임2#태그/...`", color=0x62c1cc)
embed.set_thumbnail(url="https://i.ibb.co/4f1nw7T/P-S.webp?type=w800")
await temp_message.delete()
await msg.channel.send(embed=embed)
return
await temp_message.delete()
temp_message = await msg.channel.send("아이디 확인 중 ...") # 임시 메시지 저장
nametag_puuid = {}
for player in players:
player_name, player_tag = player.split('#')
puuid = await checkID(session, player_name, player_tag)
if puuid == -1:
embed = discord.Embed(
title=f"'{player}' 라는 유저가 존재하지 않아요 ㅜㅜ 유저분들의 닉네임과 태그를 다시 확인해주세요! (띄어쓰기, 영어 대소문자)",
description="`ex) 닉네임1#태그1/닉네임2#태그/...`", color=0x62c1cc)
embed.set_thumbnail(url="https://i.ibb.co/4f1nw7T/P-S.webp?type=w800")
await msg.channel.send(embed=embed)
await temp_message.delete() # 임시 메시지 삭제
return
nametag_puuid[player] = puuid
await temp_message.delete() # 임시 메시지 삭제
puuid_list = list(nametag_puuid.values())
player_wish_list = {puuid: [] for puuid in puuid_list}
for puuid in puuid_list:
name_tag = [name for name, p in nametag_puuid.items() if p == puuid][0]
while True:
temp_message = await msg.channel.send(
f"`{name_tag}`님이 원하시는 라인을 30초 내에 입력해주세요.\n`ex) 탑/정글/미드/원딜/서폿`") # 임시 메시지 저장
try:
line_response = await client.wait_for('message', timeout=30, check=check)
await line_response.delete()
except asyncio.TimeoutError:
await temp_message.delete()
embed = discord.Embed(title="시간 초과입니다 ㅜㅜ ",
description="저를 기다리고 계신 분들이 계시니.. 나중에 다시 시도해주세요!", color=0x62c1cc)
embed.set_thumbnail(url="https://i.ibb.co/4f1nw7T/P-S.webp?type=w800")
await msg.channel.send(embed=embed)
return
wish_lines = line_response.content.split('/')
valid_lines = ['탑', '정글', '미드', '원딜', '서폿']
valid = all(line in valid_lines for line in wish_lines)
await temp_message.delete()
if valid:
player_wish_list[puuid] = wish_lines
break
else:
await msg.channel.send("입력 형식을 다시 확인해주세요!\n`ex) 탑/미드/원딜`")
temp_message = await msg.channel.send("팀 빌딩 중 ...") # 임시 메시지 저장
t1 = t.time()
embed = await teambuild(session, nametag_puuid, player_wish_list)
if embed == -1:
await temp_message.delete()
embed = discord.Embed(title="적절한 팀 구성을 찾을 수 없습니다.",
description="각 라인당 중복되지 않은 최소 ' 2 '명의 플레이어가 입력되어야 합니다!")
embed.set_thumbnail(url="https://i.ibb.co/4f1nw7T/P-S.webp?type=w800")
await msg.channel.send(embed=embed)
return
await msg.channel.send(embed=embed)
await temp_message.delete() # 임시 메시지 삭제
t2 = t.time()
embed = discord.Embed(title="데이터 출처", description="Riot API", color=0x62c1cc)
embed.add_field(name="소요시간", value="`" + str(round(t2 - t1, 3)) + "초`", inline=False)
embed.set_footer(text="플레이어들의 티어를 분석하여 팀을 빌딩한 결과입니다.",
icon_url="https://i.ibb.co/4f1nw7T/P-S.webp?type=w800")
await msg.channel.send(embed=embed)
'''
for line in lines:
while True:
await msg.channel.send(f"{line}을 가고 싶은 유저들의 닉네임과 태그를 입력해주시고, 다른 라인과 중복 입력 가능합니다!"
f"\n(ex. 닉네임1#태그1/닉네임2#태그2)")
def check(m):
return m.author == msg.author and m.channel == msg.channel
try:
response = await client.wait_for('message', timeout=300, check=check)
except asyncio.TimeoutError:
await msg.delete()
embed = discord.Embed(title="시간 초과입니다 ㅜㅜ ",
description="저를 기다리고 계신 분들이 계시니.. 나중에 다시 시도해주세요!", color=0x62c1cc)
embed.set_thumbnail(url="https://i.ibb.co/4f1nw7T/P-S.webp?type=w800")
await msg.channel.send(embed=embed)
return
temp_message = await msg.channel.send("아이디 확인 중 ...") # 임시 메시지 저장
players = response.content.split('/')
puuids = []
valid = True
for player in players:
parts = player.split('#', 1)
if len(parts) != 2:
await temp_message.delete() # 임시 메시지 삭제
await msg.channel.send(f"`{player}`가 형식에 맞지 않아요 ㅜㅜ (ex. 닉네임1#태그1)")
valid = False
break
player_name = parts[0]
player_tag = parts[1]
puuid = await checkID(session, player_name, player_tag)
if puuid == -1:
await temp_message.delete() # 임시 메시지 삭제
await msg.channel.send(f"'{player}'라는 유저가 존재하지 않아요 ㅜㅜ (ex. 닉네임1#태그1/닉네임2#태그2)")
valid = False
break
puuids.append(puuid)
if valid:
await temp_message.delete() # 임시 메시지 삭제
players_lists[line] = puuids
break
temp_message = await msg.channel.send("팀 빌딩 중 ...") # 임시 메시지 저장
t1 = t.time()
embed = await teambuild(session, top=players_lists['탑'], jungle=players_lists['정글'], mid=players_lists['미드'],
bottom=players_lists['원딜'], support=players_lists['서폿'])
await msg.channel.send(embed=embed)
await temp_message.delete() # 임시 메시지 삭제
t2 = t.time()
embed = discord.Embed(title="데이터 출처", description="Riot API", color=0x62c1cc)
embed.add_field(name="소요시간", value="`" + str(round(t2 - t1, 3)) + "초`", inline=False)
embed.set_footer(text="플레이어들의 티어를 분석하여 팀을 빌딩한 결과입니다.",
icon_url="https://i.ibb.co/4f1nw7T/P-S.webp?type=w800")
await msg.channel.send(embed=embed)
'''
client.run(TOKEN)