@@ -44,11 +44,14 @@ def on_ready(): | |||||
print('User: ' + client.user.name) | print('User: ' + client.user.name) | ||||
print('ID: ' + client.user.id) | print('ID: ' + client.user.id) | ||||
# populate D&D spells list | |||||
for spell in requests.get('http://dnd5eapi.co/api/spells/').json()['results']: | |||||
spellslist[spell['name']] = spell['url'] | |||||
# set "Now Playing" to print version | # set "Now Playing" to print version | ||||
game = discord.Game(name=version) | game = discord.Game(name=version) | ||||
yield from client.change_presence(game=game) | yield from client.change_presence(game=game) | ||||
# called when message received | # called when message received | ||||
@client.event | @client.event | ||||
@asyncio.coroutine | @asyncio.coroutine | ||||
@@ -13,6 +13,8 @@ import re | |||||
import requests | import requests | ||||
import random | import random | ||||
import subprocess | import subprocess | ||||
import itertools | |||||
import ftfy | |||||
# LOCAL IMPORTS | # LOCAL IMPORTS | ||||
from common import * | from common import * | ||||
@@ -197,6 +199,52 @@ def cmd_roll(client, msg): | |||||
@asyncio.coroutine | @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): | def cmd_qr(client, msg): | ||||
tmp = msg.content[4:] | tmp = msg.content[4:] | ||||
@@ -358,6 +406,7 @@ commands = { | |||||
"sayy": cmd_sayy, | "sayy": cmd_sayy, | ||||
"markov": cmd_markov, | "markov": cmd_markov, | ||||
"roll": cmd_roll, | "roll": cmd_roll, | ||||
"spell": cmd_spell, | |||||
"qr": cmd_qr, | "qr": cmd_qr, | ||||
"np": cmd_np, | "np": cmd_np, | ||||
"steam": cmd_steam, | "steam": cmd_steam, | ||||
@@ -10,7 +10,7 @@ import os | |||||
import json | import json | ||||
# bot version | # bot version | ||||
version = "v1.2.3" | |||||
version = "v1.3.0" | |||||
# TODO: generate this on the fly and make it look acceptable | # TODO: generate this on the fly and make it look acceptable | ||||
# text shown by .help command | # text shown by .help command | ||||
@@ -27,6 +27,7 @@ My commands are: | |||||
**.sayy <msg>** | say something a e s t h e t i c a l l y | **.sayy <msg>** | say something a e s t h e t i c a l l y | ||||
**.markov [<user>]** | generate markov chain over chat history for you or another user | **.markov [<user>]** | generate markov chain over chat history for you or another user | ||||
**.roll <num>d<val>** | roll x number of y sided dice | **.roll <num>d<val>** | roll x number of y sided dice | ||||
**.spell <term>** | search for a D&D spell | |||||
**.qr <msg>** | generate a QR code | **.qr <msg>** | generate a QR code | ||||
**.np [<user>]** | fetch now playing from last.fm for you or a specific username | **.np [<user>]** | fetch now playing from last.fm for you or a specific username | ||||
**.steam [<user>]** | fetch steam status for you or a specific vanityname | **.steam [<user>]** | fetch steam status for you or a specific vanityname | ||||
@@ -43,3 +44,5 @@ if os.path.isfile('hist.json'): | |||||
# quiet modes | # quiet modes | ||||
quiet = {} | quiet = {} | ||||
spellslist = {} |
@@ -42,6 +42,14 @@ def logger(): | |||||
logger.addHandler(handler) | logger.addHandler(handler) | ||||
# fuzzy search over dictionary | |||||
def search(dict, term): | |||||
s = term.lower() | |||||
for item in dict.items(): | |||||
if s in item[0].lower(): | |||||
yield item | |||||
# send_message wrapper (deals with Discord's shit API) | # send_message wrapper (deals with Discord's shit API) | ||||
@asyncio.coroutine | @asyncio.coroutine | ||||
def discord_send(client, message, response): | def discord_send(client, message, response): | ||||