Skip to content

Commit

Permalink
Merge pull request #117 from janssensjelle/feature/academy-support-co…
Browse files Browse the repository at this point in the history
…mmand

[FEATURE] - Add option to `/support` for sending Academy support link
  • Loading branch information
dimoschi authored Nov 22, 2024
2 parents 4a529b8 + af88953 commit 4e0fec2
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 16 deletions.
33 changes: 20 additions & 13 deletions src/cmds/core/other.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging

import discord
from discord import ApplicationContext, Embed, Interaction, Message, WebhookMessage, slash_command
from discord import ApplicationContext, Embed, Interaction, Message, Option, WebhookMessage, slash_command
from discord.ext import commands
from discord.ui import InputText, Modal
from slack_sdk.webhook import WebhookClient
Expand All @@ -13,28 +13,30 @@


class FeedbackModal(Modal):
"""Feedback modal."""

def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)

self.add_item(InputText(label="Title"))
self.add_item(InputText(label="Feedback", style=discord.InputTextStyle.long))

async def callback(self, interaction: discord.Interaction):

async def callback(self, interaction: discord.Interaction) -> None:
"""Callback for the feedback modal."""
await interaction.response.send_message("Thank you, your feedback has been recorded.", ephemeral=True)

webhook = WebhookClient(settings.SLACK_WEBHOOK) # Establish Slack Webhook
if interaction.user: # Protects against some weird edge-cases
webhook = WebhookClient(settings.SLACK_WEBHOOK) # Establish Slack Webhook

if interaction.user: # Protects against some weird edge-cases
title = f"{self.children[0].value} - {interaction.user.name}"
else:
title = f"{self.children[0].value}"

message_body = self.children[1].value
# Slack has no way to disallow @(@everyone calls), so we strip it out and replace it with a safe version
title = title.replace("@", "[at]").replace("<", "[bracket]")
message_body = message_body.replace("@", "[at]").replace("<", "[bracket]")
title = title.replace("@", "[at]").replace("<", "[bracket]")
message_body = message_body.replace("@", "[at]").replace("<", "[bracket]")

response = webhook.send(
text=f"{title} - {message_body}",
blocks=[
Expand Down Expand Up @@ -72,9 +74,14 @@ async def no_hints(
description="A simple reply proving a link to the support desk article on how to get support")
@commands.cooldown(1, 60, commands.BucketType.user)
async def support(
self, ctx: ApplicationContext
self, ctx: ApplicationContext,
platform: Option(str, "Select the platform", choices=["labs", "academy"], default="labs"),
) -> Message:
"""A simple reply proving a link to the support desk article on how to get support"""
"""A simple reply providing a link to the support desk article on how to get support."""
if platform == "academy":
return await ctx.respond(
"https://help.hackthebox.com/en/articles/5987511-contacting-academy-support"
)
return await ctx.respond(
"https://help.hackthebox.com/en/articles/5986762-contacting-htb-support"
)
Expand All @@ -95,7 +102,7 @@ async def spoiler(self, ctx: ApplicationContext, url: str) -> Interaction | Webh
@slash_command(guild_ids=settings.guild_ids, description="Provide feedback to HTB!")
@commands.cooldown(1, 60, commands.BucketType.user)
async def feedback(self, ctx: ApplicationContext) -> Interaction:
""" Provide Feedback to HTB """
"""Provide Feedback to HTB."""
# Send the Modal defined above in Feedback Modal, which handles the callback
modal = FeedbackModal(title="Feedback")
return await ctx.send_modal(modal)
Expand Down
42 changes: 39 additions & 3 deletions tests/src/cmds/core/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,57 @@ async def test_no_hints(self, bot, ctx):
assert content.startswith("No hints are allowed")

@pytest.mark.asyncio
async def test_support(self, bot, ctx):
async def test_support_labs(self, bot, ctx):
"""Test the response of the `support` command."""
cog = other.OtherCog(bot)
ctx.bot = bot
platform = "labs"

# Invoke the command.
await cog.support.callback(cog, ctx)
await cog.support.callback(cog, ctx, platform)

args, kwargs = ctx.respond.call_args
content = args[0]

# Command should respond with a string.
assert isinstance(content, str)

assert content.startswith("https://help.hackthebox.com")
assert content == "https://help.hackthebox.com/en/articles/5986762-contacting-htb-support"

@pytest.mark.asyncio
async def test_support_academy(self, bot, ctx):
"""Test the response of the `support` command."""
cog = other.OtherCog(bot)
ctx.bot = bot
platform = "academy"

# Invoke the command.
await cog.support.callback(cog, ctx, platform)

args, kwargs = ctx.respond.call_args
content = args[0]

# Command should respond with a string.
assert isinstance(content, str)

assert content == "https://help.hackthebox.com/en/articles/5987511-contacting-academy-support"

@pytest.mark.asyncio
async def test_support_urls_different(self, bot, ctx):
"""Test that the URLs for 'labs' and 'academy' platforms are different."""
cog = other.OtherCog(bot)
ctx.bot = bot

# Test the 'labs' platform
await cog.support.callback(cog, ctx, "labs")
labs_url = ctx.respond.call_args[0][0]

# Test the 'academy' platform
await cog.support.callback(cog, ctx, "academy")
academy_url = ctx.respond.call_args[0][0]

# Assert that the URLs are different
assert labs_url != academy_url

@pytest.mark.asyncio
async def test_spoiler_without_url(self, bot, ctx):
Expand Down

0 comments on commit 4e0fec2

Please sign in to comment.