Added jisho api interface
This commit is contained in:
parent
3567edfc17
commit
7c0e643ecc
|
@ -1,5 +1,6 @@
|
|||
import discord
|
||||
from discord.ext import commands
|
||||
from utils import jisho
|
||||
|
||||
class Search(commands.Cog):
|
||||
def __init__(self, bot: commands.Bot):
|
||||
|
@ -11,7 +12,14 @@ class Search(commands.Cog):
|
|||
if query == None:
|
||||
return
|
||||
|
||||
await ctx.send(query)
|
||||
response = jisho.query(query)
|
||||
await ctx.send(response["data"][0]["slug"])
|
||||
|
||||
@search.error
|
||||
async def search_error(self, ctx, error):
|
||||
if isinstance(error, commands.CommandOnCooldown):
|
||||
pass # Suppress that annoying exception everytime someone is on cooldown
|
||||
|
||||
|
||||
|
||||
def setup(bot: commands.Bot):
|
||||
|
|
14
utils/jisho.py
Normal file
14
utils/jisho.py
Normal file
|
@ -0,0 +1,14 @@
|
|||
import requests
|
||||
import urllib.parse
|
||||
import json
|
||||
|
||||
TEMPLATE_URL = "https://jisho.org/api/v1/search/words?keyword={0}"
|
||||
|
||||
def query(query: str):
|
||||
url = TEMPLATE_URL.format(urllib.parse.quote_plus(query))
|
||||
r = requests.get(url)
|
||||
|
||||
if r.status_code != 200:
|
||||
print(f"ERROR: Failed to access Jisho API... {r.status_code}")
|
||||
|
||||
return json.loads(r.text)
|
Loading…
Reference in a new issue