-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
148 lines (102 loc) · 3.62 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import discord
import os
import requests
import json
import concurrent
from dotenv import load_dotenv
load_dotenv()
def request_get(url):
return requests.get(url)
def check_valid_status_code(request):
if request.status_code == 200:
return request.json()
return False
def get_matches():
url = "https://api.opendota.com/api/players/{0}/recentMatches".format(
os.getenv('STEAM'))
response = requests.get(url)
if not check_valid_status_code(response):
print(response.status_code)
return False
matches = check_valid_status_code(response)
matches_list = []
for match in matches:
matches_list.append((match["match_id"], match["player_slot"]))
return matches_list
def get_sa_games():
matches_list = get_matches()
match_urls = []
for match in matches_list:
match_urls.append(
"https://api.opendota.com/api/matches/{0}".format(match[0]))
with concurrent.futures.ThreadPoolExecutor() as executor:
match_response = [executor.submit(
request_get, url) for url in match_urls]
concurrent.futures.wait(match_response)
my_response = []
for i in range(len(matches_list)):
data = match_response[i].result().json()
players = data["players"]
for player in players:
if player["player_slot"] == matches_list[i][1]:
my_response.append(player)
return my_response
def get_toxic():
toxic_list = []
for response in get_sa_games():
item_list = []
for i in range(6):
item_list.append(response["item_{0}".format(i)])
for j in range(3):
item_list.append(response["backpack_{0}".format(j)])
for item in item_list:
if item == 215:
toxic_list.append((response, "bought a Shadow Amulet"))
empty_inv = 0
for item in item_list:
empty_inv += item
if empty_inv == 0:
toxic_list.append((response, "destroyed all his items"))
# response["leaver_status"] == 2 Bryce told me to add this
if response["leaver_status"] == 2 or response["leaver_status"] == 3:
toxic_list.append((response, "abandoned"))
return toxic_list
def create_message():
with open('heroes.json') as heroes_files:
heroes = json.load(heroes_files)["heroes"]
heroes_files.close()
tlist = get_toxic()
if len(tlist) != 0:
toxic = tlist[0][0]
else:
return ("Jacko has been very well behaved, no toxicity found!", False)
hero_id = toxic["hero_id"]
kills = toxic["kills"]
deaths = toxic["deaths"]
match_id = toxic["match_id"]
reason = tlist[0][1]
for hero in heroes:
if hero["id"] == hero_id:
hero_name = hero["localized_name"]
text = "Jacko recently {0} in a game as {1}, dying {2} times and getting {3} kills.".format(
reason, hero_name, deaths, kills)
dotabuff_url = "https://www.dotabuff.com/matches/{0}".format(match_id)
return (text, dotabuff_url)
client = discord.Client()
@ client.event
async def on_ready():
for guild in client.guilds:
if guild.name == os.getenv('DISCORD_GUILD'):
break
print(f'{client.user} has connected to {guild.name}')
@ client.event
async def on_message(message):
if message.author == client.user:
return
if message.content == '!shadow':
async with message.channel.typing():
response = create_message()
await message.channel.send(response[0])
if response[1]:
await message.channel.send(response[1])
client.run(os.getenv('TOKEN'))