|
|
|
@ -1,11 +1,9 @@
|
|
|
|
|
import asyncio
|
|
|
|
|
import random
|
|
|
|
|
import urllib.parse
|
|
|
|
|
|
|
|
|
|
from aiohttp import request
|
|
|
|
|
from discord.ext import commands
|
|
|
|
|
from discord.ext.commands import BucketType, cooldown, command
|
|
|
|
|
|
|
|
|
|
from cogs.anime.interactive import error_function
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Set up the cog
|
|
|
|
|
class eightball(commands.Cog):
|
|
|
|
@ -15,34 +13,24 @@ class eightball(commands.Cog):
|
|
|
|
|
@command(name="8ball", aliases=['8Ball'])
|
|
|
|
|
@cooldown(1, 1, BucketType.user)
|
|
|
|
|
async def _8ball(self, ctx, *, question):
|
|
|
|
|
"""Allows for the user to get a custom response to a question"""
|
|
|
|
|
|
|
|
|
|
# Setting up the channels that the commands can be sent in enso-chan-commands and general
|
|
|
|
|
channels = ["enso-chan-commands", "picto-chat"]
|
|
|
|
|
"""8ball responses!"""
|
|
|
|
|
|
|
|
|
|
# Surround with try/except to catch any exceptions that may occur
|
|
|
|
|
try:
|
|
|
|
|
# Make the text readable to the api
|
|
|
|
|
eightball_question = urllib.parse.quote(question)
|
|
|
|
|
|
|
|
|
|
# If the channel that the command has been sent is in the list of accepted channels
|
|
|
|
|
if str(ctx.channel) in channels:
|
|
|
|
|
|
|
|
|
|
# Open the file containing all the custom eightball responses
|
|
|
|
|
with open('images/FunCommands/eightball.txt', encoding="utf8") as file:
|
|
|
|
|
# Store the eightball responses in an array
|
|
|
|
|
eightball_array = file.readlines()
|
|
|
|
|
# Repeat the user question and send out a random response from _8ball_array
|
|
|
|
|
await ctx.send(f'Question: {question}\nAnswer: {random.choice(eightball_array)}')
|
|
|
|
|
|
|
|
|
|
# else the command is sent in an invalid channel
|
|
|
|
|
else:
|
|
|
|
|
# Using API, make a connection to 8ball API
|
|
|
|
|
async with request("GET", f"https://8ball.delegator.com/magic/JSON/{eightball_question}",
|
|
|
|
|
headers={}) as response:
|
|
|
|
|
|
|
|
|
|
# Call error_function() and display it to the user
|
|
|
|
|
message = await ctx.send(error_function())
|
|
|
|
|
# With a successful connection
|
|
|
|
|
# Get the answer
|
|
|
|
|
if response.status == 200:
|
|
|
|
|
data = await response.json()
|
|
|
|
|
api_question = data["magic"]
|
|
|
|
|
api_answer = api_question["answer"]
|
|
|
|
|
|
|
|
|
|
# Let the user read the message for 2.5 seconds
|
|
|
|
|
await asyncio.sleep(2.5)
|
|
|
|
|
# Delete the message
|
|
|
|
|
await message.delete()
|
|
|
|
|
await ctx.send(api_answer)
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(e)
|
|
|
|
|