-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands.py
413 lines (321 loc) · 11.5 KB
/
commands.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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# Maki
# ----
# Discord bot by MrDetonia
#
# Copyright 2018 Zac Herd
# Licensed under BSD 3-clause License, see LICENSE.md for more info
# IMPORTS
import asyncio
import os
import sys
import re
import requests
import random
import subprocess
import itertools
import ftfy
# LOCAL IMPORTS
from common import *
from helpers import *
from secret import lfmkey, steamkey
import markov
# COMMAND IMPLEMENTATIONS
@asyncio.coroutine
def cmd_help(client, msg):
yield from discord_send(client, msg, helptext)
@asyncio.coroutine
def cmd_info(client, msg):
pyver = "{}.{}.{}".format(sys.version_info[0], sys.version_info[1],
sys.version_info[2])
appinfo = yield from client.application_info()
response = "I am **{}**, a Discord bot by **{}** | `{}` | Python `{}` | discord.py `{}`".format(
appinfo.name, appinfo.owner.name, version, pyver, discord.__version__)
yield from discord_send(client, msg, response)
@asyncio.coroutine
def cmd_upskirt(client, msg):
response = "No, don\'t look at my pantsu, baka! <https://github.com/MrDetonia/maki>"
yield from discord_send(client, msg, response)
whoistring = "**{}#{}**: `{}`\n**Account Created:** `{}`"
@asyncio.coroutine
def cmd_whoami(client, msg):
response = whoistring.format(msg.author.name,
msg.author.discriminator, msg.author.id,
strfromdt(msg.author.created_at))
yield from discord_send(client, msg, response)
@asyncio.coroutine
def cmd_whois(client, msg):
tmp = msg.content[7:]
user = msg.server.get_member_named(tmp)
if user == None:
reponse = "I can't find `{}`".format(tmp)
else:
response = whoistring.format(user.name, user.discriminator, user.id,
strfromdt(user.created_at))
yield from discord_send(client, msg, response)
@asyncio.coroutine
def cmd_seen(client, msg):
tmp = msg.content[6:]
user = msg.server.get_member_named(tmp)
if user == None:
reponse = "I can't find `{}`".format(tmp)
elif user.name == "Maki":
reponse = "I'm right here!"
else:
target = msg.server.id + user.id
if target in history and history[target][0] == msg.server.id:
response = "**{}** was last seen saying the following at {}:\n{}".format(
user.name, strfromdt(dtfromts(history[target][1])),
history[target][2])
else:
response = "I haven't seen **{}** speak yet!".format(tmp)
yield from discord_send(client, msg, response)
@asyncio.coroutine
def cmd_say(client, msg):
response = msg.content[5:]
yield from client.delete_message(msg)
yield from discord_send(client, msg, response)
@asyncio.coroutine
def cmd_sayy(client, msg):
response = " ".join(msg.content[6:])
yield from client.delete_message(msg)
yield from discord_send(client, msg, response)
@asyncio.coroutine
def cmd_markov(client, msg):
yield from discord_typing(client, msg)
tmp = msg.content[8:]
target = ""
if tmp == "Maki":
response = "My markovs always say the same thing"
else:
if tmp == "":
target = "{}-{}".format(msg.server.id, msg.author.id)
else:
try:
target = "{}-{}".format(msg.server.id,
msg.server.get_member_named(tmp).id)
except AttributeError:
reponse = "I can't find `{}`".format(tmp)
if target != "":
mfile = "./persist/markovs/" + target
if os.path.isfile(mfile):
mc = markov.Markov(open(mfile))
response = mc.generate_text(random.randint(20, 40))
else:
response = "I haven't seen `{}` speak yet.".format(tmp)
yield from discord_send(client, msg, response)
@asyncio.coroutine
def cmd_roll(client, msg):
tmp = msg.content[6:]
pattern = re.compile("^(\d+)d(\d+)([+-]\d+)?(.*)?$")
pattern2 = re.compile("^d(\d+)([+-]\d+)?(.*)?$")
# extract numbers
nums = [int(s) for s in re.findall(r"\d+", tmp)]
if pattern.match(tmp):
numdice = nums[0]
diceval = nums[1]
elif pattern2.match(tmp):
numdice = 1
diceval = nums[0]
else:
response = "Expected format: `[<num>]d<value>[{+-}<modifier>]`"
yield from discord_send(client, msg, response)
# extract modifier, if any
modifier = 0
modpattern = re.compile("^(\d+)?d(\d+)[+-]\d+(.*)?$")
if modpattern.match(tmp):
modifier = nums[len(nums) - 1]
# negate modifier, if necessary
modpattern = re.compile("^(\d+)?d(\d+)\-\d+(.*)?$")
if modpattern.match(tmp):
modifier = -modifier
# limit ranges
numdice = clamp(numdice, 1, 100)
diceval = clamp(diceval, 1, 1000)
# roll and sum dice
rolls = []
for i in range(numdice):
rolls.append(random.randint(1, diceval))
rollsum = sum(rolls) + modifier
# generate response text
response = "**{} rolled:** {}d{}".format(msg.author.display_name, numdice,
diceval)
if modifier > 0:
response += "+{}".format(modifier)
if modifier < 0:
response += "{}".format(modifier)
response += "\n**Rolls:** `{}`".format(rolls)
response += "\n**Result:** `{}`".format(rollsum)
if rollsum - modifier == numdice * diceval:
response += " *(Natural - confirmed `{}`)*".format(
random.randint(1, 20))
elif rollsum - modifier == numdice:
response += " *(Crit fail - confirmed `{}`)*".format(
random.randint(1, 20))
yield from discord_send(client, msg, response)
@asyncio.coroutine
def cmd_spell(client, msg):
searchterm = msg.content[7:]
# perform search on user input
results = []
for result in itertools.islice(search(spellslist, searchterm), 3):
results.append(result)
# default response is an error
response = "Couldn't find any matching spells!"
# otherwise, grab spell data and generate response text
if results:
result = requests.get(results[0][1]).json()
response = "**Spell:** " + result['name']
if result['concentration'] is "yes":
response += " *(C)*"
response += " " + str(result['components'])
response += "\n\n**Level:** " + str(result['level'])
response += "\n\n**Description:**"
for s in result['desc']:
response += '\n' + s
if 'higher_level' in result:
response += "\n\n**Higher Level:**"
for s in result['higher_level']:
response += '\n' + s
response += "\n\n**Range:** " + result['range']
response += "\n\n**Casting Time:** " + result['casting_time']
response += "\n\n**Duration:** " + result['duration']
# repair encoding errors from API
response = ftfy.fix_text(response)
# append next search matches, if any
matches = []
for k,_ in results[1:]:
matches.append(k)
if matches:
response += "\n\n*Possible Matches: " + str(matches) + "*"
yield from discord_send(client, msg, response)
@asyncio.coroutine
def cmd_qr(client, msg):
tmp = msg.content[4:]
yield from discord_typing(client, msg)
# generate qr code
qr = subprocess.Popen(
"qrencode -t png -o -".split(),
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
qr.stdin.write(tmp.encode("utf-8"))
qr.stdin.close()
out = subprocess.check_output(
"curl -F upload=@- https://w1r3.net".split(), stdin=qr.stdout)
response = out.decode("utf-8").strip()
yield from discord_send(client, msg, response)
@asyncio.coroutine
def cmd_np(client, msg):
tmp = msg.content[4:]
if tmp == "":
response = lastfm_np(msg.author.name)
else:
response = lastfm_np(tmp)
print("CALLING SEND")
yield from discord_send(client, msg, response)
@asyncio.coroutine
def cmd_steam(client, msg):
tmp = msg.content[7:]
if tmp == "":
response = steamdata(msg.author.name)
else:
response = steamdata(tmp)
yield from discord_send(client, msg, response)
# HELPER FUNCTIONS
# gets now playing information from last.fm
def lastfm_np(username):
# sanitise username
cleanusername = re.sub(r'[^a-zA-Z0-9_-]', '', username, 0)
# fetch JSON from last.fm
payload = {
'format': 'json',
'method': 'user.getRecentTracks',
'user': cleanusername,
'limit': '1',
'api_key': lfmkey
}
r = requests.get("http://ws.audioscrobbler.com/2.0/", params=payload)
# read json data
np = r.json()
# check we got a valid response
if 'error' in np:
return "I couldn't get last.fm data for `{}`".format(username)
# get fields
try:
username = np['recenttracks']['@attr']['user']
track = np['recenttracks']['track'][0]
album = track['album']['#text']
artist = track['artist']['#text']
song = track['name']
nowplaying = '@attr' in track
except IndexError:
return "It looks like `{}` hasn't played anything recently.".format(
username)
# grammar
if album != "":
albumtext = "` from the album `{}`".format(album)
else:
albumtext = "`"
if nowplaying == True:
nowplaying = " is listening"
else:
nowplaying = " last listened"
# construct string
return "{}{} to `{}` by `{}{}".format(username, nowplaying, song, artist,
albumtext)
# gets general steam user info from a vanityurl name
def steamdata(vanityname):
# sanitise username
cleanvanityname = re.sub(r'[^a-zA-Z0-9_-]', '', vanityname, 0)
resolveurl = 'http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key='
dataurl = 'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key='
# fetch json from steam
try:
idresponse = requests.get(resolveurl + steamkey + '&vanityurl=' +
vanityname).json()['response']
except:
return "I can't connect to Steam"
# check if user was found and extract steamid
if idresponse['success'] is not 1:
return "I couldn't find `{}`".format(vanityname)
else:
steamid = idresponse['steamid']
# fetch steam user info
try:
dataresponse = requests.get(dataurl + steamkey + '&steamids=' +
steamid).json()['response']['players'][0]
except:
return "Can't find info on `{}`".format(vanityname)
personastates = [
'Offline', 'Online', 'Busy', 'Away', 'Snoozed', 'Looking to trade',
'Looking to play'
]
if 'personaname' in dataresponse: namestr = dataresponse['personaname']
else: namestr = ''
if 'personastate' in dataresponse:
statestr = '`' + personastates[dataresponse['personastate']] + '`'
else:
statestr = ''
if 'gameextrainfo' in dataresponse:
gamestr = ' playing `' + dataresponse['gameextrainfo'] + '`'
else:
gamestr = ''
responsetext = [(namestr + ' is ' + statestr + gamestr).replace(' ', ' ')]
return '\n'.join(responsetext)
# COMMAND HANDLING
prefix = "."
commands = {
"help": cmd_help,
"info": cmd_info,
"upskirt": cmd_upskirt,
"whoami": cmd_whoami,
"whois": cmd_whois,
"seen": cmd_seen,
"say": cmd_say,
"sayy": cmd_sayy,
"markov": cmd_markov,
"roll": cmd_roll,
"spell": cmd_spell,
"qr": cmd_qr,
"np": cmd_np,
"steam": cmd_steam,
}