From c3b04f85364ca60d3ee5d6f925e68b951b031cb7 Mon Sep 17 00:00:00 2001 From: wizzdom Date: Tue, 29 Oct 2024 17:27:17 +0000 Subject: [PATCH] add /fortune command (#50) --- requirements.txt | 1 + src/extensions/fortune.py | 45 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 src/extensions/fortune.py diff --git a/requirements.txt b/requirements.txt index 5a7cb3d..9f9d34c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ ruff==0.2.0 pre-commit==3.6.0 python-dotenv==1.0.1 pyfiglet==1.0.2 +fortune-python==1.1.1 diff --git a/src/extensions/fortune.py b/src/extensions/fortune.py new file mode 100644 index 0000000..8f581c6 --- /dev/null +++ b/src/extensions/fortune.py @@ -0,0 +1,45 @@ +import arc +import hikari +from fortune import fortune + +fortune_cmd = arc.GatewayPlugin(name="fortune") + + +@fortune_cmd.include +@arc.slash_command("fortune", "Send a user a random Fortune!") +async def fortune_command( + ctx: arc.GatewayContext, + user: arc.Option[hikari.User, arc.UserParams("A user")] = None, +) -> None: + """Send a random Fortune!""" + + # generate fortune + fortune_message = fortune() + if not fortune_message: + await ctx.respond( + "❌ Failed to generate fortune. Please try again.", + flags=hikari.MessageFlag.EPHEMERAL, + ) + return + + # mention user if present + if user is not None: + message = f"Dear {user.mention},\n```{fortune_message}```" + else: + message = f"```{fortune_message}```" + + # do not exceed Discord's 2000 character limit + if len(message) > 2000: + await ctx.respond( + "❌ FThe generated fortune is too long to send. Please try again.", + flags=hikari.MessageFlag.EPHEMERAL, + ) + return + + # send fortune in a codeblock + await ctx.respond(message) + + +@arc.loader +def loader(client: arc.GatewayClient) -> None: + client.add_plugin(fortune_cmd)