Skip to content

Commit cf501f5

Browse files
feat: add raw-format command and tip
1 parent 77c45ba commit cf501f5

File tree

5 files changed

+184
-1
lines changed

5 files changed

+184
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "message-manager-backend",
3-
"version": "2.2.1",
3+
"version": "2.3.0",
44
"description": "Backend for the message manager site",
55
"main": "./dist/index.js",
66
"exports": "./dist/index.js",

src/discord_commands/global.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@
164164
},
165165
{
166166
"default_permission": true,
167+
"dm_permission": true,
167168
"type": 1,
168169
"name": "info",
169170
"description": "Information Commands",
@@ -176,5 +177,52 @@
176177
"required": true
177178
}
178179
]
180+
},
181+
{
182+
"type": 1,
183+
"name": "raw-format",
184+
"description": "Get the raw format of user, channel and role mentions. Use this format to show mentions in messages",
185+
"dm_permission": true,
186+
"options": [
187+
{
188+
"type": 1,
189+
"name": "user",
190+
"description": "Get the raw format of a user mention",
191+
"options": [
192+
{
193+
"type": 6,
194+
"name": "user",
195+
"required": true,
196+
"description": "The user to get the raw format of"
197+
}
198+
]
199+
},
200+
{
201+
"type": 1,
202+
"name": "role",
203+
"description": "Get the raw format of a role mention",
204+
"options": [
205+
{
206+
"type": 8,
207+
"name": "role",
208+
"required": true,
209+
"description": "The role to get the raw format of"
210+
}
211+
]
212+
},
213+
{
214+
"type": 1,
215+
"name": "channel",
216+
"description": "Get the raw format of a channel mention",
217+
"options": [
218+
{
219+
"type": 7,
220+
"name": "channel",
221+
"required": true,
222+
"description": "The channel to get the raw format of"
223+
}
224+
]
225+
}
226+
]
179227
}
180228
]
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import {
2+
APIApplicationCommandInteractionDataChannelOption,
3+
APIApplicationCommandInteractionDataRoleOption,
4+
APIApplicationCommandInteractionDataSubcommandOption,
5+
APIApplicationCommandInteractionDataUserOption,
6+
APIChatInputApplicationCommandGuildInteraction,
7+
ApplicationCommandOptionType,
8+
InteractionResponseType,
9+
MessageFlags,
10+
} from "discord-api-types/v9";
11+
12+
import {
13+
InteractionOrRequestFinalStatus,
14+
UnexpectedFailure,
15+
} from "../../../errors";
16+
import { InternalInteractionType } from "../../interaction";
17+
import { InteractionReturnData } from "../../types";
18+
19+
// eslint-disable-next-line @typescript-eslint/require-await
20+
export default async function handleRawFormatCommand(
21+
internalInteraction: InternalInteractionType<APIChatInputApplicationCommandGuildInteraction>
22+
): Promise<InteractionReturnData> {
23+
const interaction = internalInteraction.interaction;
24+
const subcommand = interaction.data.options?.[0];
25+
if (
26+
!subcommand ||
27+
subcommand.type !== ApplicationCommandOptionType.Subcommand
28+
) {
29+
throw new UnexpectedFailure(
30+
InteractionOrRequestFinalStatus.APPLICATION_COMMAND_MISSING_EXPECTED_OPTION,
31+
"Missing subcommand"
32+
);
33+
}
34+
switch (subcommand.name) {
35+
case "user":
36+
return handleRawFormatUserSubcommand(internalInteraction, subcommand);
37+
38+
case "role":
39+
return handleRawFormatRoleSubcommand(internalInteraction, subcommand);
40+
41+
case "channel":
42+
return handleRawFormatChannelSubcommand(internalInteraction, subcommand);
43+
44+
default:
45+
throw new UnexpectedFailure(
46+
InteractionOrRequestFinalStatus.APPLICATION_COMMAND_UNEXPECTED_SUBCOMMAND,
47+
`Invalid subcommand: \`${subcommand.name}\``
48+
);
49+
}
50+
}
51+
52+
function handleRawFormatUserSubcommand(
53+
internalInteraction: InternalInteractionType<APIChatInputApplicationCommandGuildInteraction>,
54+
subcommand: APIApplicationCommandInteractionDataSubcommandOption
55+
): InteractionReturnData {
56+
const targetId: string | undefined = (
57+
subcommand.options?.find(
58+
(option) =>
59+
option.name === "user" &&
60+
option.type === ApplicationCommandOptionType.User
61+
) as APIApplicationCommandInteractionDataUserOption | undefined
62+
)?.value;
63+
if (targetId === undefined) {
64+
throw new UnexpectedFailure(
65+
InteractionOrRequestFinalStatus.APPLICATION_COMMAND_MISSING_EXPECTED_OPTION,
66+
"Missing target option"
67+
);
68+
}
69+
70+
return {
71+
type: InteractionResponseType.ChannelMessageWithSource,
72+
data: {
73+
content: `\`<@${targetId}>\``,
74+
flags: MessageFlags.Ephemeral,
75+
},
76+
};
77+
}
78+
function handleRawFormatRoleSubcommand(
79+
internalInteraction: InternalInteractionType<APIChatInputApplicationCommandGuildInteraction>,
80+
subcommand: APIApplicationCommandInteractionDataSubcommandOption
81+
): InteractionReturnData {
82+
const targetId: string | undefined = (
83+
subcommand.options?.find(
84+
(option) =>
85+
option.name === "role" &&
86+
option.type === ApplicationCommandOptionType.Role
87+
) as APIApplicationCommandInteractionDataRoleOption | undefined
88+
)?.value;
89+
if (targetId === undefined) {
90+
throw new UnexpectedFailure(
91+
InteractionOrRequestFinalStatus.APPLICATION_COMMAND_MISSING_EXPECTED_OPTION,
92+
"Missing target option"
93+
);
94+
}
95+
96+
return {
97+
type: InteractionResponseType.ChannelMessageWithSource,
98+
data: {
99+
content: `\`<@&${targetId}>\``,
100+
flags: MessageFlags.Ephemeral,
101+
},
102+
};
103+
}
104+
105+
function handleRawFormatChannelSubcommand(
106+
internalInteraction: InternalInteractionType<APIChatInputApplicationCommandGuildInteraction>,
107+
subcommand: APIApplicationCommandInteractionDataSubcommandOption
108+
): InteractionReturnData {
109+
const targetId: string | undefined = (
110+
subcommand.options?.find(
111+
(option) =>
112+
option.name === "channel" &&
113+
option.type === ApplicationCommandOptionType.Channel
114+
) as APIApplicationCommandInteractionDataChannelOption | undefined
115+
)?.value;
116+
if (targetId === undefined) {
117+
throw new UnexpectedFailure(
118+
InteractionOrRequestFinalStatus.APPLICATION_COMMAND_MISSING_EXPECTED_OPTION,
119+
"Missing target option"
120+
);
121+
}
122+
return {
123+
type: InteractionResponseType.ChannelMessageWithSource,
124+
data: {
125+
content: `\`<#${targetId}>\``,
126+
flags: MessageFlags.Ephemeral,
127+
},
128+
};
129+
}

src/interactions/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import handleConfigCommand from "./commands/chatInput/config";
5050
import handleInfoCommand, {
5151
handleInfoAutocomplete,
5252
} from "./commands/chatInput/info";
53+
import handleRawFormatCommand from "./commands/chatInput/raw-format";
5354
import handleSendCommand from "./commands/chatInput/send";
5455
import handleActionMessageCommand from "./commands/message/actions";
5556
import handleAddMessageMessageCommand from "./commands/message/addMessage";
@@ -573,6 +574,7 @@ const interactionsPlugin = async (instance: FastifyInstance) => {
573574
handler.addGuildOnlyCommand("config", handleConfigCommand);
574575
handler.addGuildOnlyCommand("actions", handleActionsCommand);
575576
handler.addGuildOnlyCommand("add-message", handleAddMessageCommand);
577+
handler.addGuildOnlyCommand("raw-format", handleRawFormatCommand);
576578

577579
// Add message commands to handler
578580
handler.addGuildOnlyMessageCommand("actions", handleActionMessageCommand);

src/lib/tips/tips.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ const allTips: Tip[] = [
1414
{
1515
message: "Discord administrators always have all bot permissions",
1616
},
17+
{
18+
message:
19+
"Use /raw-format to get the format for mentions to show up in messages!",
20+
},
1721
];
1822

1923
export { allTips };

0 commit comments

Comments
 (0)