1
0
mirror of https://github.com/MrDetonia/Maki.git synced 2025-04-20 16:51:07 -04:00

Compare commits

...

2 Commits

Author SHA1 Message Date
Zac Herd
5541a7c128 v1.3.0, added .spell command to search for D&D 5e spells 2019-02-26 14:22:57 +00:00
Zac Herd
5c1afd1b73 fixed minimum version requirements and tidied dockerfile 2019-02-26 14:22:21 +00:00
6 changed files with 73 additions and 3 deletions

View File

@ -1,5 +1,11 @@
FROM python:3.5-alpine
# copy files
WORKDIR /maki
COPY ["requirements.txt", "*.py", "./"]
# install dependencies
RUN pip install -r requirements.txt
# start bot
CMD ["python", "./bot.py"]

5
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

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,

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 = {}

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):

View File

@ -1,2 +1,3 @@
discord.py==0.15.1
discord.py>=0.16.0
requests>=2.20.0
ftfy>=5.5.0