aboutsummaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorGravatar dngfx2018-09-23 09:52:53 +0100
committerGravatar dngfx2018-09-23 09:52:53 +0100
commitfdcf3e45c70daffb314e3533fafb66aedbba6b1a (patch)
tree84f67796779e44ec8268203518309eb9d8530c07 /modules
parentAdd setcoins command for admins, for people that abuse the coin game, or rewa... (diff)
signature
Add .randomword command, and change the api to use https
Diffstat (limited to 'modules')
-rw-r--r--modules/define.py61
1 files changed, 56 insertions, 5 deletions
diff --git a/modules/define.py b/modules/define.py
index 3451aa5f..ae1aa7b2 100644
--- a/modules/define.py
+++ b/modules/define.py
@@ -1,24 +1,42 @@
#--require-config wordnik-api-key
import Utils
+import time
-URL_WORDNIK = "http://api.wordnik.com:80/v4/word.json/%s/definitions"
+URL_WORDNIK = "https://api.wordnik.com/v4/word.json/%s/definitions"
+URL_WORDNIK_RANDOM = "https://api.wordnik.com/v4/words.json/randomWord"
+
+RANDOM_DELAY_SECONDS = 3
class Module(object):
def __init__(self, bot, events, exports):
self.bot = bot
+ self.last_called = 0
+ self.events = events
+
events.on("received.command.define").hook(self.define,
help="Define a provided term", usage="<phrase>")
+ events.on("received.command.randomword").hook(self.random_word,
+ help="Generate a random word!")
+
+ def get_definition(self, event):
+ word = event["args"] if "args" in event else event
+
+
+ page = Utils.get_url(URL_WORDNIK % word, get_params={
+ "useCanonical": "true", "limit": 1,
+ "sourceDictionaries": "wiktionary", "api_key": self.bot.config[
+ "wordnik-api-key"]}, json=True)
+
+ return page
+
def define(self, event):
if event["args"]:
word = event["args"]
else:
word = event["buffer"].get(from_self=False)
- page = Utils.get_url(URL_WORDNIK % event["args"], get_params={
- "useCanonical": "true", "limit": 1,
- "sourceDictionaries": "wiktionary", "api_key": self.bot.config[
- "wordnik-api-key"]}, json=True)
+ page = self.get_definition(event)
if page:
if len(page):
event["stdout"].write("%s: %s" % (page[0]["word"],
@@ -27,3 +45,36 @@ class Module(object):
event["stderr"].write("No definitions found")
else:
event["stderr"].write("Failed to load results")
+
+ def random_word(self, event):
+ if not self.last_called or (time.time()-self.last_called >=
+ RANDOM_DELAY_SECONDS):
+
+ self.last_called = time.time()
+
+ page = Utils.get_url(URL_WORDNIK_RANDOM, get_params={
+ "api_key":self.bot.config["wordnik-api-key"],
+ "min_dictionary_count":1},json=True)
+ if page:
+ if len(page):
+ definition = self.get_definition(page["word"])
+
+ if len(definition):
+ definition = definition[0]
+ else:
+ self.events.on("send.stderr").call(module_name="Random",
+ target=event["target"],
+ message="Try again in a couple of seconds")
+ return
+
+ event["stdout"].set_prefix("Random")
+ event["stdout"].write("Random Word: %s - Definition: %s" % (
+ page["word"], definition["text"]))
+ else:
+ event["stderr"].write("Something has gone terribly wrong")
+ else:
+ event["stderr"].write("Failed to load results")
+ else:
+ self.events.on("send.stderr").call(module_name="Random",
+ target=event["target"],
+ message="Try again in a couple of seconds") \ No newline at end of file