-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHardstuckBot.py
155 lines (140 loc) · 7.41 KB
/
HardstuckBot.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
149
150
151
152
153
154
155
import os, discord, json, random, asyncio, requests
with open(os.path.dirname(__file__) + "/config.json") as f:
config = json.load(f)
f.close()
def getRank(summoner):
with open(os.path.dirname(__file__) + "/config.json") as f:
data = json.load(f)
apiKey = data["riotKey"]
try:
summoner = json.loads(requests.get("https://na1.api.riotgames.com/lol/summoner/v4/summoners/by-name/%s?api_key=%s" % (summoner, apiKey)).content)
data = json.loads(requests.get("https://na1.api.riotgames.com/lol/league/v4/entries/by-summoner/%s?api_key=%s" % (summoner['id'], apiKey)).content)
for i in data:
if i["queueType"] == "RANKED_SOLO_5x5":
rank = {
"name": summoner["name"],
"profile": summoner["profileIconId"],
"tier": i["tier"][0] + i["tier"][1:].lower(),
"rank": i["rank"],
"lp": i["leaguePoints"],
"winrate": round(i["wins"] / (i["wins"] + i["losses"]) * 100, 1),
"games": i["wins"] + i["losses"]
}
return rank
except:
return None
def generateEmbed(data):
colors = {
"Iron": 0x8e898b,
"Bronze": 0x7d553e,
"Silver": 0x646a64,
"Gold": 0xe3c44b,
"Platinum": 0x275752,
"Diamond": 0x5587c3,
"Master": 0xce56fe,
"Grandmaster": 0xc31f2d,
"Challenger": 0x1f6da5
}
embed=discord.Embed(title=data["name"], color=colors[data["tier"]])
embed.set_thumbnail(url="http://ddragon.leagueoflegends.com/cdn/9.23.1/img/profileicon/" + str(data["profile"]) + ".png")
embed.add_field(name="Games", value=data["games"], inline=False)
embed.add_field(name="Rank", value="%s %s" % (data["tier"], data["rank"]), inline=False)
embed.add_field(name="LP", value=data["lp"], inline=False)
embed.add_field(name="Winrate", value=str(data["winrate"]) + "%", inline=False)
return embed
class botClient(discord.Client):
async def update(self):
while True:
with open(os.path.dirname(__file__) + "/stalkList.json") as f:
slist = json.load(f)
for i in slist["stalkList"]:
try:
data = getRank(i["summoner"])
if data is None:
await self.get_channel(i["id"]).send("%s not found..." % i["summoner"])
else:
await self.get_channel(i["id"]).send(embed=generateEmbed(data))
if data["winrate"] < 49:
await message.channel.send(random.choice(config["shame"]["poor"]))
elif data["winrate"] < 51:
await message.channel.send(random.choice(config["shame"]["average"]))
else:
await message.channel.send(random.choice(config["shame"]["good"]))
except:
print("Error.")
f.close()
await asyncio.sleep(86400)
async def on_ready(self):
print('Logged on as {0}!'.format(self.user))
await self.update()
async def on_message(self, message):
if message.author == client.user:
return
if message.content.startswith(config["prefix"]):
cmd = message.content.split(" ")[0][len(config["prefix"]):]
args = message.content.split(" ")[1:]
if cmd == "shame":
if len(args) == 0:
await message.channel.send("Please enter a summoner name to shame")
else:
summoner = " ".join(args)
data = getRank(summoner)
if data is None:
await message.channel.send("%s not found..." % (summoner))
else:
await message.channel.send(embed=generateEmbed(data))
if data["winrate"] < 49:
await message.channel.send(random.choice(config["shame"]["poor"]))
elif data["winrate"] < 51:
await message.channel.send(random.choice(config["shame"]["average"]))
else:
await message.channel.send(random.choice(config["shame"]["good"]))
if cmd == "stalk":
if not message.author.permissions_in(message.channel).administrator:
await message.channel.send("You must be an administrator to stalk people. Don't ask why.")
else:
summoner = " ".join(args)
data = getRank(summoner)
if(data):
with open(os.path.dirname(__file__) + "/stalkList.json", "r") as f:
slist = json.load(f)
stalking = False
for i in slist["stalkList"]:
if message.channel.id == i["id"] and data["name"] == i["summoner"]:
await message.channel.send("Already stalking %s in channel %s" % (i["summoner"], message.channel))
stalking = True
if not stalking:
await message.channel.send("Now stalking %s in %s" % (data["name"], message.channel.name))
entry = {
"id": message.channel.id,
"summoner": data["name"]
}
slist["stalkList"].append(entry)
f.close()
with open(os.path.dirname(__file__) + "/stalkList.json", "w") as f:
json.dump(slist, f)
f.close()
else:
await message.channel.send("%s not found..." % (summoner))
if cmd == "stop":
if len(args) == 0:
await message.channel.send("Removing all targets from channel %s" % (message.channel))
with open(os.path.dirname(__file__) + "/stalkList.json", "r") as f:
slist = json.load(f)
slist["stalkList"] = [x for x in slist["stalkList"] if not x["id"] == message.channel.id]
f.close()
with open(os.path.dirname(__file__) + "/stalkList.json", "w") as f:
json.dump(slist, f)
f.close()
else:
summoner = " ".join(args)
await message.channel.send("Removing %s from channel %s" % (summoner, message.channel))
with open(os.path.dirname(__file__) + "/stalkList.json", "r") as f:
slist = json.load(f)
slist["stalkList"] = [x for x in slist["stalkList"] if not (x["id"] == message.channel.id and x["summoner"].lower() == summoner.lower())]
f.close()
with open(os.path.dirname(__file__) + "/stalkList.json", "w") as f:
json.dump(slist, f)
f.close()
client = botClient()
client.run(config["botKey"])