Browse Source

v1.3.0, added .spell command to search for D&D 5e spells

master
Zac Herd 5 years ago
parent
commit
5541a7c128
4 changed files with 65 additions and 2 deletions
  1. +4
    -1
      bot.py
  2. +49
    -0
      commands.py
  3. +4
    -1
      common.py
  4. +8
    -0
      helpers.py

+ 4
- 1
bot.py View File

@@ -44,11 +44,14 @@ def on_ready():
print('User: ' + client.user.name)
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
game = discord.Game(name=version)
yield from client.change_presence(game=game)


# called when message received
@client.event
@asyncio.coroutine


+ 49
- 0
commands.py View File

@@ -13,6 +13,8 @@ import re
import requests
import random
import subprocess
import itertools
import ftfy

# LOCAL IMPORTS
from common import *
@@ -197,6 +199,52 @@ def cmd_roll(client, msg):


@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:]

@@ -358,6 +406,7 @@ commands = {
"sayy": cmd_sayy,
"markov": cmd_markov,
"roll": cmd_roll,
"spell": cmd_spell,
"qr": cmd_qr,
"np": cmd_np,
"steam": cmd_steam,


+ 4
- 1
common.py View File

@@ -10,7 +10,7 @@ import os
import json

# bot version
version = "v1.2.3"
version = "v1.3.0"

# TODO: generate this on the fly and make it look acceptable
# 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
**.markov [<user>]** | generate markov chain over chat history for you or another user
**.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
**.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
@@ -43,3 +44,5 @@ if os.path.isfile('hist.json'):

# quiet modes
quiet = {}

spellslist = {}

+ 8
- 0
helpers.py View File

@@ -42,6 +42,14 @@ def logger():
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)
@asyncio.coroutine
def discord_send(client, message, response):


Loading…
Cancel
Save