-
Notifications
You must be signed in to change notification settings - Fork 15
Adding wolfram group of commands to gurkbot #106
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
Open
dhzdhd
wants to merge
10
commits into
gurkult:main
Choose a base branch
from
dhzdhd:feature/wolfram
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3bc6027
Adding wolfram group of commands to gurkbot
dhzdhd a4aa34c
Merge branch 'gurkult:main' into feature/wolfram
dhzdhd 5255214
Migrate to slash commands and apply suggested changes
dhzdhd 8217db8
Fix closing of bot http_session on command re-execution
dhzdhd 3eabda1
Remove wolfram emoji, change embed color and resolve param suggestion
dhzdhd ff37499
Remove unnecessary guild ID
dhzdhd 369e392
Fix exposed server location and exposed token
dhzdhd 2d573ef
Add optional themes for generated image
dhzdhd 222b69e
Merge branch 'main' into feature/wolfram
Akarys42 0052bcb
Remove docstring for non implemented commands and sort imports
dhzdhd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,3 +18,5 @@ EMOJI_TRASHCAN="" | |
| ROLE_STEERING_COUNCIL="" | ||
| ROLE_MODERATORS="" | ||
| ROLE_DEVOPS="" | ||
|
|
||
| WOLFRAM_TOKEN="" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import random | ||
| from io import BytesIO | ||
| from typing import Literal, Union | ||
| from urllib import parse | ||
|
|
||
| import disnake | ||
| from disnake import Embed | ||
| from disnake.ext import commands | ||
|
|
||
| from bot.constants import ERROR_REPLIES, Colours, Tokens | ||
|
|
||
|
|
||
| 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. | ||
| """ | ||
|
|
||
| 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", | ||
| "layout": "labelbar", | ||
| "fontsize": "23", | ||
| "width": "700", | ||
| "location": "the moon", | ||
| "latlong": "0.0,0.0", | ||
| "ip": "1.1.1.1", | ||
| } | ||
|
|
||
| async def web_request(self, url: str, params: dict) -> Union[bytes, 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 await resp.read() | ||
| 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, | ||
| theme: Literal["light", "dark"] = "dark", | ||
| ) -> None: | ||
| """ | ||
| Send wolfram image corresponding to the given query. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| query: The user query. | ||
| theme: The theme of the image generated. dark by default. | ||
| """ | ||
| await inter.response.defer() | ||
|
|
||
| params = self.params | ||
|
|
||
| if theme == "light": | ||
| params |= {"background": "white", "foreground": "2F3136"} | ||
|
|
||
| response = await self.web_request( | ||
| url=self.image_url, params=params | {"i": query} | ||
| ) | ||
|
|
||
| if isinstance(response, str): | ||
| embed = Embed(title=random.choice(ERROR_REPLIES), description=response) | ||
| else: | ||
| original_url = parse.quote_plus(query) | ||
| file = disnake.File(BytesIO(response), filename="image.png") | ||
|
|
||
| embed = Embed(title="Wolfram Alpha", colour=Colours.green) | ||
| embed.set_image(file=file) | ||
| 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)) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.