forked from jeffffc/wwstatsbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
285 lines (226 loc) · 11.3 KB
/
Copy pathmain.py
File metadata and controls
285 lines (226 loc) · 11.3 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# wolfcardbot.py - Extracts Werewolf for Telegram Stats & Displays in Chat
# author - Carson True
# license - GPL
# edited by @jeffffc
# /search by @jamiscs
import requests
import logging
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater
from telegram.ext import CommandHandler
from telegram.ext.dispatcher import run_async
import datetime
import json
import html
from unidecode import unidecode
from config import BOT_TOKEN, LOG_GROUP_ID
import wwstats
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
def get_stats(user_id):
wuff_url = "http://www.tgwerewolf.com/Stats/PlayerStats/?pid={}&json=true"
stats = requests.get(wuff_url.format(user_id)).json()
return stats
def get_achievement_count(user_id):
wuff_url = "http://www.tgwerewolf.com/Stats/PlayerAchievements/?pid={}&json=true"
r = requests.get(wuff_url.format(user_id)).json()
return len(r)
def get_kills(user_id):
wuff_url = "http://www.tgwerewolf.com/Stats/PlayerKills/?pid={}&json=true"
kills = requests.get(wuff_url.format(user_id)).json()
return kills
def get_killed_by(user_id):
wuff_url = "http://www.tgwerewolf.com/Stats/PlayerKilledBy/?pid={}&json=true"
killedby = requests.get(wuff_url.format(user_id)).json()
return killedby
def get_deaths(user_id):
wuff_url = "http://www.tgwerewolf.com/Stats/PlayerDeaths/?pid={}&json=true"
deaths = requests.get(wuff_url.format(user_id)).json()
return deaths
def get_achievements(user_id):
wuff_url = "http://www.tgwerewolf.com/Stats/PlayerAchievements/?pid={}&json=true"
r = requests.get(wuff_url.format(user_id)).json()
return r
@run_async
def display_kills(bot, update):
chat_id = update.message.chat_id
if update.message.reply_to_message is not None:
user_id = update.message.reply_to_message.from_user.id
name = update.message.reply_to_message.from_user.first_name
name = html.escape(name)
else:
user_id = update.message.from_user.id
name = update.message.from_user.first_name
name = html.escape(name)
print("%s - %s (%d) - kills" % (str(datetime.datetime.now()+datetime.timedelta(hours=8)), unidecode(name), user_id))
kills = get_kills(user_id)
msg = "Players <a href='tg://user?id={}'> {}</a> most killed:\n".format(user_id, name)
for n in range(len(kills)):
msg += "<code>{:<5}</code> <b>{}</b>\n".format(kills[n]['times'], html.escape(kills[n]['name']))
bot.sendMessage(chat_id, msg, parse_mode="HTML", disable_web_page_preview=True)
@run_async
def display_killed_by(bot, update):
chat_id = update.message.chat_id
if update.message.reply_to_message is not None:
user_id = update.message.reply_to_message.from_user.id
name = update.message.reply_to_message.from_user.first_name
name = html.escape(name)
else:
user_id = update.message.from_user.id
name = update.message.from_user.first_name
name = html.escape(name)
print("%s - %s (%d) - killed by" % (str(datetime.datetime.now()+datetime.timedelta(hours=8)), unidecode(name), user_id))
killedby = get_killed_by(user_id)
msg = "Players who killed <a href='tg://user?id={}'>{}</a> most:\n".format(user_id, name)
for n in range(len(killedby)):
msg += "<code>{:<5}</code> <b>{}</b>\n".format(killedby[n]['times'], html.escape(killedby[n]['name']))
bot.sendMessage(chat_id, msg, parse_mode="HTML", disable_web_page_preview=True)
@run_async
def display_deaths(bot, update):
chat_id = update.message.chat_id
if update.message.reply_to_message is not None:
user_id = update.message.reply_to_message.from_user.id
name = update.message.reply_to_message.from_user.first_name
name = html.escape(name)
else:
user_id = update.message.from_user.id
name = update.message.from_user.first_name
name = html.escape(name)
print("%s - %s (%d) - deaths" % (str(datetime.datetime.now()+datetime.timedelta(hours=8)), unidecode(name), user_id))
deaths = get_deaths(user_id)
stats = get_stats(user_id)
msg = "Types of deaths that <a href='tg://user?id={}'>{}</a> most had:\n".format(user_id, name)
for n in range(len(deaths)):
""" The total of deaths for each kill method is calculated based on the percentage
gave by the JSON data. Because of that, the calculated value is not totally accurate."""
totalMethod = ((stats['gamesPlayed']-stats['survived']['total'])*float(deaths[n]['percent'])/100)
msg += "<code>{}%</code> <b>{}</b> <code>(approx. {})</code>\n".format(deaths[n]['percent'],deaths[n]['method'],round(totalMethod))
"""msg += "<code>({}%)</code> <b>{}</b>\n".format(deaths[n]['percent'],deaths[n]['method'])"""
bot.sendMessage(chat_id, msg, parse_mode="HTML", disable_web_page_preview=True)
@run_async
def display_search(bot, update, args):
chat_id = update.message.chat_id
if update.message.reply_to_message is not None:
user_id = update.message.reply_to_message.from_user.id
name = update.message.reply_to_message.from_user.first_name
name = html.escape(name)
else:
user_id = update.message.from_user.id
name = update.message.from_user.first_name
name = html.escape(name)
print("%s - %s (%d) - search %s" % (str(datetime.datetime.now()+datetime.timedelta(hours=8)), unidecode(name), user_id, args))
if len(args) == 0:
msg = "Invalid parameter! Syntax:\n<code>/search [achievement_to_search]</code>\n"
else:
found_counter = 0
achv_name = ""
achv = get_achievements(user_id)
msg = "Attained achievements of <a href='tg://user?id={}'>{}</a> found:\n".format(user_id, name)
for item in range(len(achv)):
achv_name = "{}".format(achv[item]['name'])
for n in range(len(achv_name.split())):
for word in range(len(args)):
if achv_name.split()[n].lower().startswith(args[word].lower()):
msg += "<code>{}</code>\n".format(achv_name)
found_counter+=1
break
if found_counter == 0:
msg += "<b>No matching achievements found!</b>\n"
bot.sendMessage(chat_id, msg, parse_mode="HTML", disable_web_page_preview=True)
@run_async
def display_stats(bot, update, args):
by_id = False
chat_id = update.message.chat_id
if update.message.reply_to_message is not None:
user_id = update.message.reply_to_message.from_user.id
name = update.message.reply_to_message.from_user.first_name
name = html.escape(name)
else:
if args:
try:
user_id = int(args[0])
name = args[0]
by_id = True
except:
user_id = update.message.from_user.id
name = update.message.from_user.first_name
name = html.escape(name)
else:
user_id = update.message.from_user.id
name = update.message.from_user.first_name
name = html.escape(name)
print("%s - %s (%d) - stats" % (str(datetime.datetime.now()+datetime.timedelta(hours=8)), unidecode(name), user_id))
stats = get_stats(user_id)
achievements = get_achievement_count(user_id)
if stats:
msg = "<a href='tg://user?id={}'>{} the {}</a>\n".format(user_id, name, stats['mostCommonRole']) if not by_id else "{} the {}\n".format(name, stats['mostCommonRole'])
msg += "<code>{:<5}</code> Achievements Unlocked!\n".format(achievements)
msg += "<code>{:<5}</code> Games Won <code>({}%)</code>\n".format(stats['won']['total'], stats['won']['percent'])
msg += "<code>{:<5}</code> Games Lost <code>({}%)</code>\n".format(stats['lost']['total'], stats['lost']['percent'])
msg += "<code>{:<5}</code> Games Survived <code>({}%)</code>\n".format(
stats['survived']['total'], stats['survived']['percent'])
msg += "<code>{:<5}</code> Total Games\n".format(stats['gamesPlayed'])
msg += "<code>{:<5}</code> times I've gleefully killed {}\n".format(
stats['mostKilled']['times'], html.escape(stats['mostKilled']['name']))
msg += "<code>{:<5}</code> times I've been slaughted by {}\n\n".format(
stats['mostKilledBy']['times'], html.escape(stats['mostKilledBy']['name']))
else:
msg = "<a href='tg://user?id={}'>{}</a> has not played any games.".format(user_id, name) if not by_id else "{} has not played any games.".format(name)
bot.sendMessage(chat_id, msg, parse_mode="HTML", disable_web_page_preview=True)
def display_about(bot, update):
chat_id = update.message.chat_id
msg = "Use /stats for stats. Use /achievements or /achv for achivement list."
msg += "\n\nThis is an edited version to the old `@wolfcardbot`.\n"
msg += "Click [here](http://pastebin.com/efZ4CPXJ) to check the original source code.\n"
msg += "Click [here](https://github.com/jeffffc/wwstatsbot) for the source code of the current project."
bot.sendMessage(chat_id, msg, parse_mode="Markdown", disable_web_page_preview=True)
def startme(bot, update):
if update.message.chat.type == 'private':
update.message.reply_text("Thank you for starting me. "
"Use /stats and /achievements to check your related stats!")
else:
return
@run_async
def display_achv(bot, update):
user_id = update.message.from_user.id
name = update.message.from_user.first_name
name = html.escape(name)
print("%s - %s (%d) - achv" % (str(datetime.datetime.now()+datetime.timedelta(hours=8)), unidecode(name), user_id))
msgs = wwstats.check(user_id)
try:
for msg in msgs:
bot.sendMessage(chat_id = user_id, text=msg, parse_mode='Markdown')
if update.message.chat.type != 'private':
update.message.reply_text("I have sent you your achievement list in PM.")
except:
url = "telegram.me/{}".format(bot.username)
keyboard = [[InlineKeyboardButton("Start Me!", url=url)]]
reply_markup = InlineKeyboardMarkup(keyboard)
update.message.reply_text("You have to start me in PM first.", reply_markup=reply_markup)
def error_handler(bot, update, error):
e = str(error).lower()
if "timed out" in e or "not modified" in e or "query_id_invalid" in e:
return
msg = "This update caused error.\n"
msg += "{}\n\n".format(error.msg)
msg += "```{}```".format(json.dumps(json.loads(update.message.to_json()), indent=2, ensure_ascii=False))
bot.send_message(LOG_GROUP_ID, error.msg, parse_mode='Markdown')
def main():
u = Updater(token=BOT_TOKEN)
d = u.dispatcher
d.add_handler(CommandHandler('start', startme))
d.add_handler(CommandHandler('stats', display_stats, pass_args=True))
d.add_handler(CommandHandler('kills', display_kills))
d.add_handler(CommandHandler('killedby', display_killed_by))
d.add_handler(CommandHandler('deaths', display_deaths))
d.add_handler(CommandHandler(['search', 'sch'], display_search, pass_args=True))
d.add_handler(CommandHandler('about', display_about))
d.add_handler(CommandHandler(['achievements', 'achv'], display_achv))
d.add_error_handler(error_handler)
u.start_polling(clean=True)
u.idle()
if __name__ == '__main__':
main()