Skip to content

Commit f08cdb5

Browse files
committed
refactor: extract action-related constants and utility functions into action-helpers module for better organization and reuse
1 parent a84e847 commit f08cdb5

4 files changed

Lines changed: 112 additions & 114 deletions

File tree

src/commands/ban/index.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ApplicationCommandOptionType, ChannelType } from "discord.js";
22
import { ActionReason, ActionType } from "../../../generated/prisma/index.js";
33
import { config } from "../../env.js";
44
import { createActionDetails, createActionNotification } from "../../utils/action-embeds.js";
5+
import { BAN_TYPE_CHOICES, REASON_CHOICES } from "../../utils/action-helpers.js";
56
import { createAction, getDefaultExpiration } from "../../utils/actions.js";
67
import { createSlashCommand } from "../helpers.js";
78

@@ -21,27 +22,14 @@ export const ban = createSlashCommand({
2122
description: "Ban type (permanent or temporary)",
2223
type: ApplicationCommandOptionType.String,
2324
required: true,
24-
choices: [
25-
{ name: "Permanent", value: "permanent" },
26-
{ name: "Temporary", value: "temporary" },
27-
],
25+
choices: BAN_TYPE_CHOICES,
2826
},
2927
{
3028
name: "reason",
3129
description: "The reason for the ban",
3230
type: ApplicationCommandOptionType.String,
3331
required: true,
34-
choices: [
35-
{ name: "Spam", value: ActionReason.SPAM },
36-
{ name: "Scam", value: ActionReason.SCAM },
37-
{ name: "Disruptive", value: ActionReason.DISRUPTIVE },
38-
{ name: "NSFW", value: ActionReason.NSFW },
39-
{ name: "Hate Speech", value: ActionReason.HATE_SPEECH },
40-
{ name: "Self Promotion", value: ActionReason.SELF_PROMOTION },
41-
{ name: "Job Posting", value: ActionReason.JOB_POSTING },
42-
{ name: "For Hire", value: ActionReason.FOR_HIRE },
43-
{ name: "Other", value: ActionReason.OTHER },
44-
],
32+
choices: REASON_CHOICES,
4533
},
4634
{
4735
name: "duration",

src/commands/warn/index.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ApplicationCommandOptionType, ChannelType } from "discord.js";
22
import { ActionReason, ActionType } from "../../../generated/prisma/index.js";
33
import { config } from "../../env.js";
44
import { createActionDetails, createActionNotification } from "../../utils/action-embeds.js";
5+
import { REASON_CHOICES } from "../../utils/action-helpers.js";
56
import { createAction, getDefaultExpiration } from "../../utils/actions.js";
67
import { createSlashCommand } from "../helpers.js";
78

@@ -21,17 +22,7 @@ export const warn = createSlashCommand({
2122
description: "The reason for the warning",
2223
type: ApplicationCommandOptionType.String,
2324
required: true,
24-
choices: [
25-
{ name: "Spam", value: ActionReason.SPAM },
26-
{ name: "Scam", value: ActionReason.SCAM },
27-
{ name: "Disruptive", value: ActionReason.DISRUPTIVE },
28-
{ name: "NSFW", value: ActionReason.NSFW },
29-
{ name: "Hate Speech", value: ActionReason.HATE_SPEECH },
30-
{ name: "Self Promotion", value: ActionReason.SELF_PROMOTION },
31-
{ name: "Job Posting", value: ActionReason.JOB_POSTING },
32-
{ name: "For Hire", value: ActionReason.FOR_HIRE },
33-
{ name: "Other", value: ActionReason.OTHER },
34-
],
25+
choices: REASON_CHOICES,
3526
},
3627
{
3728
name: "note",

src/utils/action-embeds.ts

Lines changed: 7 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,18 @@
11
import { EmbedBuilder, type User } from "discord.js";
22
import type { Action, User as PrismaUser } from "../../generated/prisma/index.js";
3-
import { type ActionReason, ActionStatus, ActionType } from "../../generated/prisma/index.js";
3+
import { type ActionReason, ActionStatus, type ActionType } from "../../generated/prisma/index.js";
4+
import {
5+
formatReason,
6+
getActionColor,
7+
getActionEmoji,
8+
getActionTypeName,
9+
} from "./action-helpers.js";
410

511
type ActionWithRelations = Action & {
612
user: PrismaUser;
713
moderator: PrismaUser;
814
};
915

10-
/**
11-
* Get the color for an action based on its severity
12-
*/
13-
function getActionColor(actionType: ActionType): number {
14-
switch (actionType) {
15-
case ActionType.WARN:
16-
case ActionType.REPEL:
17-
case ActionType.TIMEOUT:
18-
return 0xffff00; // Yellow
19-
case ActionType.MUTE:
20-
case ActionType.TEMP_MUTE:
21-
case ActionType.KICK:
22-
return 0xffa500; // Orange
23-
case ActionType.BAN:
24-
case ActionType.TEMP_BAN:
25-
return 0xff0000; // Red
26-
case ActionType.REVERT:
27-
return 0x00ff00; // Green
28-
default:
29-
return 0xffa500; // Default to orange
30-
}
31-
}
32-
33-
/**
34-
* Format the action reason for display
35-
*/
36-
function formatReason(reason: ActionReason): string {
37-
return reason
38-
.toLowerCase()
39-
.replace(/_/g, " ")
40-
.replace(/\b\w/g, (char: string) => char.toUpperCase());
41-
}
42-
43-
/**
44-
* Get the emoji for an action type
45-
*/
46-
function getActionEmoji(actionType: ActionType): string {
47-
switch (actionType) {
48-
case ActionType.WARN:
49-
return "⚠️";
50-
case ActionType.REPEL:
51-
return "🚫";
52-
case ActionType.TIMEOUT:
53-
return "⏱️";
54-
case ActionType.MUTE:
55-
case ActionType.TEMP_MUTE:
56-
return "🔇";
57-
case ActionType.KICK:
58-
return "👢";
59-
case ActionType.BAN:
60-
case ActionType.TEMP_BAN:
61-
return "🔨";
62-
case ActionType.REVERT:
63-
return "✅";
64-
default:
65-
return "📋";
66-
}
67-
}
68-
69-
/**
70-
* Get the action type name for display
71-
*/
72-
function getActionTypeName(actionType: ActionType): string {
73-
switch (actionType) {
74-
case ActionType.WARN:
75-
return "Warning";
76-
case ActionType.REPEL:
77-
return "Repel";
78-
case ActionType.TIMEOUT:
79-
return "Timeout";
80-
case ActionType.MUTE:
81-
return "Mute";
82-
case ActionType.TEMP_MUTE:
83-
return "Temporary Mute";
84-
case ActionType.KICK:
85-
return "Kick";
86-
case ActionType.BAN:
87-
return "Ban";
88-
case ActionType.TEMP_BAN:
89-
return "Temporary Ban";
90-
case ActionType.REVERT:
91-
return "Revert";
92-
default:
93-
return "Action";
94-
}
95-
}
96-
9716
/**
9817
* Format timestamp for display
9918
*/

src/utils/action-helpers.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { ActionReason, ActionType } from "../../generated/prisma/index.js";
2+
3+
export function getActionColor(actionType: ActionType): number {
4+
switch (actionType) {
5+
case ActionType.WARN:
6+
case ActionType.REPEL:
7+
case ActionType.TIMEOUT:
8+
// Yellow
9+
return 0xffff00;
10+
case ActionType.MUTE:
11+
case ActionType.TEMP_MUTE:
12+
case ActionType.KICK:
13+
// Orange
14+
return 0xffa500;
15+
case ActionType.BAN:
16+
case ActionType.TEMP_BAN:
17+
// Red
18+
return 0xff0000;
19+
case ActionType.REVERT:
20+
// Green
21+
return 0x00ff00;
22+
default:
23+
// Default to Orange
24+
return 0xffa500;
25+
}
26+
}
27+
28+
export function formatReason(reason: ActionReason): string {
29+
return reason
30+
.toLowerCase()
31+
.replace(/_/g, " ")
32+
.replace(/\b\w/g, (char: string) => char.toUpperCase());
33+
}
34+
35+
export function getActionEmoji(actionType: ActionType): string {
36+
switch (actionType) {
37+
case ActionType.WARN:
38+
return "⚠️";
39+
case ActionType.REPEL:
40+
return "🚫";
41+
case ActionType.TIMEOUT:
42+
return "⏱️";
43+
case ActionType.MUTE:
44+
case ActionType.TEMP_MUTE:
45+
return "🔇";
46+
case ActionType.KICK:
47+
return "👢";
48+
case ActionType.BAN:
49+
case ActionType.TEMP_BAN:
50+
return "🔨";
51+
case ActionType.REVERT:
52+
return "✅";
53+
default:
54+
return "📋";
55+
}
56+
}
57+
58+
export function getActionTypeName(actionType: ActionType): string {
59+
switch (actionType) {
60+
case ActionType.WARN:
61+
return "Warning";
62+
case ActionType.REPEL:
63+
return "Repel";
64+
case ActionType.TIMEOUT:
65+
return "Timeout";
66+
case ActionType.MUTE:
67+
return "Mute";
68+
case ActionType.TEMP_MUTE:
69+
return "Temporary Mute";
70+
case ActionType.KICK:
71+
return "Kick";
72+
case ActionType.BAN:
73+
return "Ban";
74+
case ActionType.TEMP_BAN:
75+
return "Temporary Ban";
76+
case ActionType.REVERT:
77+
return "Revert";
78+
default:
79+
return "Action";
80+
}
81+
}
82+
83+
export const BAN_TYPE_CHOICES = [
84+
{ name: "Permanent", value: "permanent" },
85+
{ name: "Temporary", value: "temporary" },
86+
];
87+
Object.freeze(BAN_TYPE_CHOICES);
88+
89+
export const REASON_CHOICES = [
90+
{ name: "Spam", value: ActionReason.SPAM },
91+
{ name: "Scam", value: ActionReason.SCAM },
92+
{ name: "Disruptive", value: ActionReason.DISRUPTIVE },
93+
{ name: "NSFW", value: ActionReason.NSFW },
94+
{ name: "Hate Speech", value: ActionReason.HATE_SPEECH },
95+
{ name: "Self Promotion", value: ActionReason.SELF_PROMOTION },
96+
{ name: "Job Posting", value: ActionReason.JOB_POSTING },
97+
{ name: "For Hire", value: ActionReason.FOR_HIRE },
98+
{ name: "Other", value: ActionReason.OTHER },
99+
];
100+
Object.freeze(REASON_CHOICES);

0 commit comments

Comments
 (0)