2022-08-10 01:04:19 +02:00
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
import discord
|
2022-07-18 23:22:28 +02:00
|
|
|
from discord import app_commands
|
2022-07-13 22:54:16 +02:00
|
|
|
from discord.ext import commands
|
|
|
|
|
2022-08-10 01:04:19 +02:00
|
|
|
from database.crud.links import get_link_by_name
|
2022-08-29 20:24:42 +02:00
|
|
|
from database.schemas import Link
|
2022-07-13 22:54:16 +02:00
|
|
|
from didier import Didier
|
2022-09-22 02:04:34 +02:00
|
|
|
from didier.data.apis import disease_sh, inspirobot, urban_dictionary
|
2022-07-18 23:22:28 +02:00
|
|
|
from didier.data.embeds.google import GoogleSearch
|
2022-09-27 11:32:19 +02:00
|
|
|
from didier.data.embeds.urban_dictionary import Definition
|
2022-07-18 23:22:28 +02:00
|
|
|
from didier.data.scrapers import google
|
2022-09-22 16:30:58 +02:00
|
|
|
from didier.utils.discord.autocompletion.country import autocomplete_country
|
2022-07-13 22:54:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Other(commands.Cog):
|
2022-09-19 18:15:10 +02:00
|
|
|
"""Commands that don't really belong anywhere else."""
|
2022-07-13 22:54:16 +02:00
|
|
|
|
|
|
|
client: Didier
|
|
|
|
|
|
|
|
def __init__(self, client: Didier):
|
|
|
|
self.client = client
|
|
|
|
|
2023-07-08 15:38:17 +02:00
|
|
|
@commands.hybrid_command(name="corona", aliases=["covid", "rona"]) # type: ignore[arg-type]
|
2022-09-22 02:04:34 +02:00
|
|
|
async def covid(self, ctx: commands.Context, country: str = "Belgium"):
|
|
|
|
"""Show Covid-19 info for a specific country.
|
|
|
|
|
|
|
|
By default, this will fetch the numbers for Belgium.
|
|
|
|
|
|
|
|
To get worldwide stats, use `all`, `global`, `world`, or `worldwide`.
|
|
|
|
"""
|
|
|
|
async with ctx.typing():
|
|
|
|
if country.lower() in ["all", "global", "world", "worldwide"]:
|
|
|
|
data = await disease_sh.get_global_info(self.client.http_session)
|
|
|
|
else:
|
|
|
|
data = await disease_sh.get_country_info(self.client.http_session, country)
|
|
|
|
|
2022-09-22 16:30:58 +02:00
|
|
|
await ctx.reply(embed=data.to_embed(), mention_author=False)
|
|
|
|
|
|
|
|
@covid.autocomplete("country")
|
|
|
|
async def _covid_country_autocomplete(self, interaction: discord.Interaction, value: str):
|
|
|
|
"""Autocompletion for the 'country'-parameter"""
|
|
|
|
return autocomplete_country(value)[:25]
|
2022-09-22 02:04:34 +02:00
|
|
|
|
2023-07-08 15:38:17 +02:00
|
|
|
@commands.hybrid_command( # type: ignore[arg-type]
|
2022-09-19 18:15:10 +02:00
|
|
|
name="define", aliases=["ud", "urban"], description="Look up the definition of a word on the Urban Dictionary"
|
|
|
|
)
|
2022-07-13 22:54:16 +02:00
|
|
|
async def define(self, ctx: commands.Context, *, query: str):
|
2022-09-19 18:15:10 +02:00
|
|
|
"""Look up the definition of `query` on the Urban Dictionary."""
|
2022-07-18 23:22:28 +02:00
|
|
|
async with ctx.typing():
|
2022-09-22 02:04:34 +02:00
|
|
|
definitions = await urban_dictionary.lookup(self.client.http_session, query)
|
2022-09-27 11:32:19 +02:00
|
|
|
await ctx.reply(
|
|
|
|
embed=definitions[0].to_embed() if definitions else Definition.no_result_found_embed(),
|
|
|
|
mention_author=False,
|
|
|
|
)
|
2022-07-18 23:22:28 +02:00
|
|
|
|
2023-07-08 15:38:17 +02:00
|
|
|
@commands.hybrid_command(name="google", description="Google search") # type: ignore[arg-type]
|
2022-07-18 23:22:28 +02:00
|
|
|
@app_commands.describe(query="Search query")
|
|
|
|
async def google(self, ctx: commands.Context, *, query: str):
|
2022-09-19 18:15:10 +02:00
|
|
|
"""Show the Google search results for `query`.
|
|
|
|
|
|
|
|
The `query`-argument can contain spaces and does not require quotes around it. For example:
|
|
|
|
```
|
|
|
|
didier query didier source github
|
|
|
|
didier query "didier source github"
|
|
|
|
```
|
|
|
|
"""
|
2022-07-18 23:22:28 +02:00
|
|
|
async with ctx.typing():
|
|
|
|
results = await google.google_search(self.client.http_session, query)
|
|
|
|
embed = GoogleSearch(results).to_embed()
|
|
|
|
await ctx.reply(embed=embed, mention_author=False)
|
2022-07-13 22:54:16 +02:00
|
|
|
|
2023-07-08 15:38:17 +02:00
|
|
|
@commands.hybrid_command(name="inspire", description="Generate an InspiroBot quote.") # type: ignore[arg-type]
|
2022-09-19 23:04:20 +02:00
|
|
|
async def inspire(self, ctx: commands.Context):
|
|
|
|
"""Generate an [InspiroBot](https://inspirobot.me/) quote."""
|
|
|
|
async with ctx.typing():
|
|
|
|
link = await inspirobot.get_inspirobot_quote(self.client.http_session)
|
|
|
|
await ctx.reply(link, mention_author=False, ephemeral=False)
|
|
|
|
|
2022-08-10 01:04:19 +02:00
|
|
|
async def _get_link(self, name: str) -> Optional[Link]:
|
|
|
|
async with self.client.postgres_session as session:
|
|
|
|
return await get_link_by_name(session, name.lower())
|
|
|
|
|
2023-07-08 15:38:17 +02:00
|
|
|
@commands.command(name="Link", aliases=["Links"]) # type: ignore[arg-type]
|
2022-08-10 01:04:19 +02:00
|
|
|
async def link_msg(self, ctx: commands.Context, name: str):
|
2022-09-19 18:15:10 +02:00
|
|
|
"""Get the link to the resource named `name`."""
|
2022-08-10 01:04:19 +02:00
|
|
|
link = await self._get_link(name)
|
|
|
|
if link is None:
|
|
|
|
return await ctx.reply(f"Found no links matching `{name}`.", mention_author=False)
|
|
|
|
|
|
|
|
target_message = await self.client.get_reply_target(ctx)
|
|
|
|
await target_message.reply(link.url, mention_author=False)
|
|
|
|
|
2023-07-08 15:38:17 +02:00
|
|
|
@app_commands.command(name="link") # type: ignore[arg-type]
|
2022-09-19 18:15:10 +02:00
|
|
|
@app_commands.describe(name="The name of the resource")
|
2022-08-10 01:04:19 +02:00
|
|
|
async def link_slash(self, interaction: discord.Interaction, name: str):
|
2022-09-19 18:15:10 +02:00
|
|
|
"""Get the link to something."""
|
2022-08-10 01:04:19 +02:00
|
|
|
link = await self._get_link(name)
|
|
|
|
if link is None:
|
|
|
|
return await interaction.response.send_message(f"Found no links matching `{name}`.", ephemeral=True)
|
|
|
|
|
|
|
|
return await interaction.response.send_message(link.url)
|
|
|
|
|
|
|
|
@link_slash.autocomplete("name")
|
2022-08-13 01:10:50 +02:00
|
|
|
async def _link_name_autocomplete(self, _: discord.Interaction, current: str) -> list[app_commands.Choice[str]]:
|
2022-08-10 01:04:19 +02:00
|
|
|
"""Autocompletion for the 'name'-parameter"""
|
2022-08-13 01:10:50 +02:00
|
|
|
return self.client.database_caches.links.get_autocomplete_suggestions(current)
|
2022-08-10 01:04:19 +02:00
|
|
|
|
2022-07-13 22:54:16 +02:00
|
|
|
|
|
|
|
async def setup(client: Didier):
|
|
|
|
"""Load the cog"""
|
|
|
|
await client.add_cog(Other(client))
|