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

add /fortune command #50

Merged
merged 4 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ hikari-arc==1.1.0
ruff==0.2.0
pre-commit==3.6.0
python-dotenv==1.0.1
fortune-python==1.1.1
45 changes: 45 additions & 0 deletions src/extensions/fortune.py
Original file line number Diff line number Diff line change
@@ -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)