Skip to content

Commit

Permalink
🗑️Convert some hybrid commands into proper slash
Browse files Browse the repository at this point in the history
  • Loading branch information
Aluerie committed Dec 28, 2024
1 parent 6491ee4 commit 4f47903
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 144 deletions.
14 changes: 9 additions & 5 deletions ext/community/emote_spam.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import discord
import emoji
from discord import app_commands
from discord.ext import commands

from bot import aluloop
Expand All @@ -16,7 +17,7 @@
from ._base import CommunityCog

if TYPE_CHECKING:
from bot import AluBot, AluContext
from bot import AluBot


class EmoteSpam(CommunityCog):
Expand Down Expand Up @@ -121,15 +122,18 @@ async def emote_spam(self) -> None:
emote = await self.get_random_emote()
await self.bot.community.emote_spam.send(f"{emote!s} {emote!s} {emote!s}")

@commands.hybrid_command()
async def do_emote_spam(self, ctx: AluContext) -> None:
@app_commands.command()
async def do_emote_spam(self, interaction: discord.Interaction[AluBot]) -> None:
"""Send 3x random emote into emote spam channel."""
emote = await self.get_random_emote()
channel = self.community.emote_spam
content = f"{emote!s} {emote!s} {emote!s}"
await channel.send(content)
e = discord.Embed(colour=const.Colour.blueviolet, description=f"I sent {content} into {channel.mention}")
await ctx.reply(embed=e, ephemeral=True, delete_after=10)
embed = discord.Embed(
colour=const.Colour.blueviolet,
description=f"I sent {content} into {channel.mention}",
)
await interaction.response.send_message(embed=embed, ephemeral=True)

@aluloop(count=1)
async def offline_criminal_check(self) -> None:
Expand Down
3 changes: 1 addition & 2 deletions ext/community/moderation.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,10 @@ async def give_aluerie_all_perms(self, channel: discord.abc.GuildChannel) -> Non
if channel.guild.id != self.community.id:
return

sister_of_the_veil = self.community.sister_of_the_veil
allow, deny = discord.Permissions.all(), discord.Permissions.none()
all_perms = discord.PermissionOverwrite.from_pair(allow=allow, deny=deny)
reason = "Give all permissions to Aluerie"
await channel.set_permissions(sister_of_the_veil, overwrite=all_perms, reason=reason)
await channel.set_permissions(self.community.sister_of_the_veil, overwrite=all_perms, reason=reason)


async def setup(bot: AluBot) -> None:
Expand Down
30 changes: 14 additions & 16 deletions ext/educational/language/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
import discord
import yarl
from discord import app_commands
from discord.ext import commands, menus
from discord.ext import menus
from lxml import html

from utils import pages
from utils import errors, pages

from .._base import EducationalCog

Expand All @@ -29,7 +29,7 @@
if TYPE_CHECKING:
from aiohttp import ClientSession

from bot import AluContext
from bot import AluBot

# from .utils.context import Context, GuildContext
# from .utils.paginator import RoboPages
Expand All @@ -40,11 +40,9 @@

def html_to_markdown(node: Any, *, include_spans: bool = False) -> str:
text = []
italics_marker = "_"
for child in node:
if child.tag == "i":
text.append(f"{italics_marker}{child.text.strip()}{italics_marker}")
italics_marker = "_" if italics_marker == "*" else "*"
text.append(f"_{child.text.strip()}_") # `_` is italics marker
elif child.tag == "b":
if text and text[-1].endswith("*"):
text.append("\u200b")
Expand Down Expand Up @@ -343,33 +341,33 @@ async def format_page(self, menu: pages.Paginator, entry: FreeDictionaryMeaning)


class DictionaryCog(EducationalCog):
@commands.hybrid_command(name="define")
@app_commands.command(name="define")
@app_commands.describe(word="The word to look up")
async def _define(self, ctx: AluContext, *, word: str) -> None:
async def _define(self, interaction: discord.Interaction[AluBot], word: str) -> None:
"""Looks up an English word in the dictionary."""
result = await parse_free_dictionary_for_word(ctx.session, word=word)
result = await parse_free_dictionary_for_word(self.bot.session, word=word)
if result is None:
await ctx.send("Could not find that word.", ephemeral=True)
return
msg = "Could not find that word."
raise errors.SomethingWentWrong(msg)

# Check if it's a phrasal verb somehow
phrase = discord.utils.find(lambda v: v.word.lower() == word.lower(), result.phrasal_verbs)
if phrase is not None:
embed = phrase.to_embed()
await ctx.send(embed=embed)
await interaction.response.send_message(embed=embed)
return

if not result.meanings:
await ctx.send("Could not find any definitions for that word.", ephemeral=True)
return
msg = "Could not find any definitions for that word."
raise errors.SomethingWentWrong(msg)

# Paginate over the various meanings of the word
p = pages.Paginator(ctx, FreeDictionaryWordMeaningPageSource(result))
p = pages.Paginator(interaction, FreeDictionaryWordMeaningPageSource(result))
await p.start()

@_define.autocomplete("word")
async def _define_word_autocomplete(
self, interaction: discord.Interaction, query: str
self, interaction: discord.Interaction[AluBot], query: str
) -> list[app_commands.Choice[str]]:
if not query:
return []
Expand Down
25 changes: 15 additions & 10 deletions ext/educational/language/translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
* RoboDanny's translator.py (license MPL v2 from Rapptz/RoboDanny)
- https://github.com/Rapptz/RoboDanny/blob/rewrite/cogs/utils/translator.py
"""

from __future__ import annotations

from typing import TYPE_CHECKING, Any, NamedTuple, TypedDict, override

import discord
from discord import app_commands
from discord.ext import commands

from utils import const, errors

Expand All @@ -35,7 +35,7 @@
if TYPE_CHECKING:
from aiohttp import ClientSession

from bot import AluBot, AluContext
from bot import AluBot


# fmt: off
Expand Down Expand Up @@ -157,12 +157,17 @@ async def translate_context_menu_callback(self, interaction: discord.Interaction
msg = "Sorry, but it seems, that this message doesn't have any text content to translate."
raise errors.BadArgument(msg)

e = await self.translate_embed(text)
await interaction.response.send_message(embed=e, ephemeral=True)
embed = await self.translate_embed(text)
await interaction.response.send_message(embed=embed, ephemeral=True)

@app_commands.command()
async def translate(self, interaction: discord.Interaction[AluBot], text: str) -> None:
"""Google Translate to English, auto-detects source language.
@commands.hybrid_command()
@app_commands.describe(text="Enter text to translate")
async def translate(self, ctx: AluContext, *, text: str) -> None:
"""Google Translate to English, auto-detects source language."""
e = await self.translate_embed(text)
await ctx.reply(embed=e)
Parameters
----------
text
Enter text to translate
"""
embed = await self.translate_embed(text)
await interaction.response.send_message(embed=embed)
71 changes: 32 additions & 39 deletions ext/educational/math/wolfram.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
from urllib import parse as urlparse

from discord import app_commands
from discord.ext import commands

from config import WOLFRAM_TOKEN
from utils import const, errors

from .._base import EducationalCog

if TYPE_CHECKING:
from bot import AluBot, AluContext
import discord

from bot import AluBot


class WolframAlphaCog(EducationalCog, emote=const.Emote.bedNerdge):
Expand All @@ -28,53 +29,45 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
self.simple_url = f"{base}/simple?appid={WOLFRAM_TOKEN}&background=black&foreground=white&layout=labelbar&i="
self.short_url = f"{base}/result?appid={WOLFRAM_TOKEN}&i="

@commands.hybrid_group(name="wolfram")
async def wolfram_group(self, ctx: AluContext) -> None:
"""WolframAlpha Commands."""
await ctx.send_help(ctx.command)
wolfram_group = app_commands.Group(
name="wolfram",
description="WolframAlpha queries.",
)

async def wa_long_worker(self, ctx: AluContext, *, query: str) -> None:
await ctx.typing()
@wolfram_group.command(name="long")
@app_commands.checks.cooldown(1, 60.0, key=lambda i: (i.guild_id, i.user.id))
async def wolfram_long(self, interaction: discord.Interaction[AluBot], query: str) -> None:
"""Get a long, detailed image-answer from WolframAlpha.
Parameters
----------
query
Query for WolframAlpha.
"""
await interaction.response.defer()
question_url = f"{self.simple_url}{urlparse.quote(query)}"
file = await self.bot.transposer.url_to_file(question_url, filename="WolframAlpha.png")
await ctx.reply(content=f"```py\n{query}```", file=file)

@wolfram_group.command(name="long")
@commands.cooldown(2, 10, commands.BucketType.user)
@app_commands.describe(query="Query for WolframAlpha.")
async def wolfram_long(self, ctx: AluContext, *, query: str) -> None:
"""Get a long, detailed image-answer from WolframAlpha."""
await self.wa_long_worker(ctx, query=query)

@commands.command(name="wolf")
@commands.cooldown(2, 10, commands.BucketType.user)
async def wolfram_long_shortcut(self, ctx: AluContext, *, query: str) -> None:
"""Just a txt command shortcut for `wolfram long`."""
await self.wa_long_worker(ctx, query=query)

async def wa_short_worker(self, ctx: AluContext, *, query: str) -> None:
await ctx.typing()
await interaction.followup.send(content=f"```py\n{query}```", file=file)

@wolfram_group.command(name="short")
@app_commands.checks.cooldown(1, 60.0, key=lambda i: (i.guild_id, i.user.id))
async def wolfram_short(self, interaction: discord.Interaction[AluBot], query: str) -> None:
"""Get a quick, short answer from WolframAlpha.
Parameters
----------
query
Query for WolframAlpha.
"""
await interaction.response.defer()
question_url = f"{self.short_url}{urlparse.quote(query)}"
async with self.bot.session.get(question_url) as response:
if response.ok:
await ctx.reply(f"```py\n{query}```{await response.text()}")
await interaction.followup.send(f"```py\n{query}```{await response.text()}")
else:
msg = f"Wolfram Response was not ok, Status {response.status},"
raise errors.ResponseNotOK(msg)

@wolfram_group.command(name="short", aliases=["wa"])
@commands.cooldown(2, 10, commands.BucketType.user)
@app_commands.describe(query="Query for WolframAlpha.")
async def wolfram_short(self, ctx: AluContext, *, query: str) -> None:
"""Get a quick, short answer from WolframAlpha."""
await self.wa_short_worker(ctx, query=query)

@commands.command(name="wa")
@commands.cooldown(2, 10, commands.BucketType.user)
async def wolfram_short_shortcut(self, ctx: AluContext, *, query: str) -> None:
"""Just a txt command shortcut for `wolfram short`."""
await self.wa_short_worker(ctx, query=query)


async def setup(bot: AluBot) -> None:
"""Load AluBot extension. Framework of discord.py."""
Expand Down
1 change: 0 additions & 1 deletion ext/fpc/base_classes/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,6 @@ async def setup_players(self, interaction: discord.Interaction[AluBot]) -> None:
# But I think it might be confusing for end-user if I have several commands to add/remove players/characters.
# One simple interactive view is enough for them granted they aren't supposed to change their preferences much.
# I'm also just hesitant to completely delete all my previous work so let's at least leave one-name version
# so we can put it as hybrid command, which is easier to maintain.

# TODO: YOINK DOCS FROM THERE

Expand Down
Loading

0 comments on commit 4f47903

Please sign in to comment.