Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding wolfram group of commands to gurkbot #106

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions .env-example
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ EMOJI_TRASHCAN=""

ROLE_STEERING_COUNCIL=""
ROLE_MODERATORS=""

WOLFRAM_TOKEN=""
5 changes: 5 additions & 0 deletions bot/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,18 @@ class Roles(NamedTuple):
events = int(os.getenv("EVENTS_ID", 890656665328820224))


class Tokens(NamedTuple):
wolfram_token = os.getenv("WOLFRAM_TOKEN")


# Bot replies
with pathlib.Path("bot/resources/bot_replies.yml").open(encoding="utf8") as file:
bot_replies = yaml.safe_load(file)
ERROR_REPLIES = bot_replies["ERROR_REPLIES"]
POSITIVE_REPLIES = bot_replies["POSITIVE_REPLIES"]
NEGATIVE_REPLIES = bot_replies["NEGATIVE_REPLIES"]


# Minecraft Server
class Minecraft(NamedTuple):
server_address = "mc.gurkult.com"
81 changes: 81 additions & 0 deletions bot/exts/fun/wolfram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import random
from typing import Union
from urllib import parse

import disnake
from bot.constants import Colours, ERROR_REPLIES, Tokens
from disnake import Embed
from disnake.ext import commands
from yarl import URL


class WolframCommands(commands.Cog):
"""
Wolfram Category cog, containing commands related to the WolframAlpha API.

Commands
├ image Fetch the response to a query in the form of an image.
├ text Fetch the response to a query in a short phrase.
└ chat Fetch the response of the Wolfram AI based on the given question/statement.
dhzdhd marked this conversation as resolved.
Show resolved Hide resolved
"""

def __init__(self, bot: commands.Bot) -> None:
self.bot = bot
self.image_url = "https://api.wolframalpha.com/v1/simple"
self.params = {
"appid": Tokens.wolfram_token,
"background": "2F3136",
"foreground": "white",
dhzdhd marked this conversation as resolved.
Show resolved Hide resolved
"layout": "labelbar",
"fontsize": "23",
"width": "700",
}

async def web_request(self, url: str, params: dict) -> Union[URL, str]:
"""Web request handler for wolfram group of commands."""
async with self.bot.http_session.get(url=url, params=params) as resp:
if resp.status == 200:
return resp.url
elif resp.status == 501:
return "Sorry, the API could not understand your input"
elif resp.status == 400:
return "Sorry, the API did not find any input to interpret"

@commands.slash_command()
async def wolfram(self, inter: disnake.ApplicationCommandInteraction) -> None:
"""Commands for wolfram."""

@wolfram.sub_command()
async def image(
self, inter: disnake.ApplicationCommandInteraction, query: str
) -> None:
"""
Send wolfram image corresponding to the given query.

Parameters
----------
query: The user query.
"""
await inter.response.defer()

response = await self.web_request(
url=self.image_url, params=self.params | {"i": query}
)

if isinstance(response, str):
embed = Embed(title=random.choice(ERROR_REPLIES), description=response)
else:
original_url = parse.quote_plus(query)
embed = Embed(title="Wolfram Alpha", colour=Colours.green)
embed.set_image(url=str(response))
dhzdhd marked this conversation as resolved.
Show resolved Hide resolved
embed.add_field(
name="Cannot see image?",
value=f"[Click here](https://www.wolframalpha.com/input/?i={original_url})",
)

await inter.edit_original_message(embed=embed)


def setup(bot: commands.Bot) -> None:
"""Load the Wolfram cog."""
bot.add_cog(WolframCommands(bot))
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ services:
- CHANNEL_LOGS
- ROLE_STEERING_COUNCIL
- ROLE_MODERATORS
- WOLFRAM_TOKEN

volumes:
- .:/bot
Expand Down