MCPcopy Index your code
hub / github.com/codebymitch/TitanBot / handleUpdate

Function handleUpdate

src/commands/ServerStats/modules/serverstats_update.js:7–83  ·  view source on GitHub ↗
(interaction, client)

Source from the content-addressed store, hash-verified

5
6import { InteractionHelper } from '../../../utils/interactionHelper.js';
7export async function handleUpdate(interaction, client) {
8 const guild = interaction.guild;
9 const counterId = interaction.options.getString("counter-id");
10 const newType = interaction.options.getString("type");
11
12 try {
13 await InteractionHelper.safeDefer(interaction);
14 } catch (error) {
15 logger.error("Failed to defer reply:", error);
16 return;
17 }
18
19 if (!interaction.member.permissions.has(PermissionFlagsBits.ManageChannels)) {
20 await replyUserError(interaction, { type: ErrorTypes.PERMISSION, message: 'You need **Manage Channels** permission to update counters.' }).catch(logger.error);
21 return;
22 }
23
24 if (!newType) {
25 await replyUserError(interaction, { type: ErrorTypes.UNKNOWN, message: 'You must provide a new counter type to update.' }).catch(logger.error);
26 return;
27 }
28
29 try {
30 const counters = await getServerCounters(client, guild.id);
31
32 const counterIndex = counters.findIndex(c => c.id === counterId);
33 if (counterIndex === -1) {
34 await replyUserError(interaction, { type: ErrorTypes.USER_INPUT, message: 'Counter with ID \\`${counterId}\\` not found. Use \\`/counter list\\` to see all counters.' }).catch(logger.error);
35 return;
36 }
37
38 const counter = counters[counterIndex];
39 const oldChannel = guild.channels.cache.get(counter.channelId);
40
41 if (!oldChannel) {
42 await replyUserError(interaction, { type: ErrorTypes.USER_INPUT, message: 'The channel for this counter no longer exists. You cannot update a counter for a deleted channel.' }).catch(logger.error);
43 return;
44 }
45
46 if (newType !== counter.type) {
47 const existingTypeCounter = counters.find(c => c.type === newType && c.id !== counter.id);
48 if (existingTypeCounter) {
49 const existingChannel = guild.channels.cache.get(existingTypeCounter.channelId);
50 await replyUserError(interaction, { type: ErrorTypes.UNKNOWN, message: '`A **${getCounterTypeLabel(newType)}** counter already exists for this server${existingChannel ? ` in ${existingChannel}` : \'\'}. Delete it first before reusing that type.`' }).catch(logger.error);
51 return;
52 }
53 }
54
55 const oldType = counter.type;
56
57 counter.type = newType;
58 counter.updatedAt = new Date().toISOString();
59
60 const saved = await saveServerCounters(client, guild.id, counters);
61 if (!saved) {
62 await replyUserError(interaction, { type: ErrorTypes.UNKNOWN, message: 'Failed to save updated counter data. Please try again.' }).catch(logger.error);
63 return;
64 }

Callers 1

executeFunction · 0.90

Calls 10

getServerCountersFunction · 0.90
saveServerCountersFunction · 0.90
updateCounterFunction · 0.90
successEmbedFunction · 0.90
getCounterEmojiFunction · 0.90
getCounterTypeLabelFunction · 0.90
replyUserErrorFunction · 0.85
safeDeferMethod · 0.80
safeEditReplyMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected