Skip to content

Commit 3545c09

Browse files
authored
Add /factoid call command (#1290)
* Add /factoid call command * Fix docstring
1 parent 31a2503 commit 3545c09

1 file changed

Lines changed: 112 additions & 0 deletions

File tree

techsupport_bot/commands/factoids.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,118 @@ async def send_to_irc(
907907
factoid_message=factoid_message,
908908
)
909909

910+
@factoid_app_group.command(
911+
name="call",
912+
description="Calls a factoid from the database and sends it publicy in the channel.",
913+
extras={
914+
"usage": "[factoid_name]",
915+
"module": "factoids",
916+
},
917+
)
918+
async def factoid_call_command(
919+
self: Self, interaction: discord.Interaction, factoid_name: str
920+
) -> None:
921+
"""This is an app command version of typing {prefix}call
922+
923+
Args:
924+
interaction (discord.Interaction): The interaction that triggered this command
925+
factoid_name (str): The factoid name to search for and print
926+
927+
Raises:
928+
TooLongFactoidMessageError: If the plaintext exceed 2000 characters
929+
"""
930+
query = factoid_name.replace("\n", " ").split(" ")[0].lower()
931+
config = self.bot.guild_configs[str(interaction.guild.id)]
932+
try:
933+
factoid = await self.get_factoid(query, str(interaction.guild.id))
934+
935+
except custom_errors.FactoidNotFoundError:
936+
embed = auxiliary.prepare_deny_embed(
937+
message=f"The factoid {factoid_name} couldn't be found"
938+
)
939+
await interaction.response.send_message(embed=embed, ephemeral=True)
940+
await self.bot.logger.send_log(
941+
message=f"Invalid factoid call {query} from {interaction.guild.id}",
942+
level=LogLevel.DEBUG,
943+
context=LogContext(
944+
guild=interaction.guild, channel=interaction.channel
945+
),
946+
)
947+
return
948+
949+
# Checking for disabled or restricted
950+
if factoid.disabled:
951+
embed = auxiliary.prepare_deny_embed(
952+
message=f"The factoid {factoid_name} is disabled."
953+
)
954+
await interaction.response.send_message(embed=embed, ephemeral=True)
955+
return
956+
957+
if (
958+
factoid.restricted
959+
and str(interaction.channel.id)
960+
not in config.extensions.factoids.restricted_list.value
961+
):
962+
embed = auxiliary.prepare_deny_embed(
963+
message=f"The factoid {factoid_name} is restricted and not allowed in this channel."
964+
)
965+
await interaction.response.send_message(embed=embed, ephemeral=True)
966+
return
967+
if not config.extensions.factoids.disable_embeds.value:
968+
embed = self.get_embed_from_factoid(factoid)
969+
else:
970+
embed = None
971+
# if the json doesn't include non embed argument, then don't send anything
972+
# otherwise send message text with embed
973+
try:
974+
content = factoid.message if not embed else None
975+
except ValueError:
976+
# The not embed causes a ValueError in certain cases. This ensures fallback works
977+
content = factoid.message
978+
979+
if content and len(content) > 2000:
980+
embed = auxiliary.prepare_deny_embed(
981+
message="I ran into an error sending that factoid: "
982+
+ "The factoid message is longer than the discord size limit (2000)",
983+
)
984+
await interaction.response.send_message(embed=embed, ephemeral=True)
985+
986+
raise custom_errors.TooLongFactoidMessageError
987+
988+
try:
989+
# define the message and send it
990+
await interaction.response.send_message(content=content, embed=embed)
991+
# log it in the logging channel with type info and generic content
992+
log_channel = config.get("logging_channel")
993+
await self.bot.logger.send_log(
994+
message=(
995+
f"Sending factoid: {query} (triggered by {interaction.user} in"
996+
f" #{interaction.channel.name})"
997+
),
998+
level=LogLevel.INFO,
999+
context=LogContext(
1000+
guild=interaction.guild, channel=interaction.channel
1001+
),
1002+
channel=log_channel,
1003+
)
1004+
# If something breaks, also log it
1005+
except discord.errors.HTTPException as exception:
1006+
log_channel = config.get("logging_channel")
1007+
await self.bot.logger.send_log(
1008+
message="Could not send factoid",
1009+
level=LogLevel.ERROR,
1010+
context=LogContext(
1011+
guild=interaction.guild, channel=interaction.channel
1012+
),
1013+
channel=log_channel,
1014+
exception=exception,
1015+
)
1016+
# Sends the raw factoid instead of the embed as fallback
1017+
await interaction.response.send_message(content=factoid.message)
1018+
await self.send_to_irc(
1019+
interaction.channel, interaction.message, factoid.message
1020+
)
1021+
9101022
# -- Factoid job related functions --
9111023
async def kickoff_jobs(self: Self) -> None:
9121024
"""Gets a list of cron jobs and starts them"""

0 commit comments

Comments
 (0)