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

Function handleCreate

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

Source from the content-addressed store, hash-verified

5
6import { InteractionHelper } from '../../../utils/interactionHelper.js';
7export async function handleCreate(interaction, client) {
8 const guild = interaction.guild;
9 const type = interaction.options.getString("type");
10 const channelType = interaction.options.getString("channel_type");
11 const category = interaction.options.getChannel("category");
12
13 try {
14 await InteractionHelper.safeDefer(interaction);
15 } catch (error) {
16 logger.error("Failed to defer reply:", error);
17 return;
18 }
19
20 if (!interaction.member.permissions.has(PermissionFlagsBits.ManageChannels)) {
21 await replyUserError(interaction, { type: ErrorTypes.PERMISSION, message: 'You need **Manage Channels** permission to create counters.' }).catch(logger.error);
22 return;
23 }
24
25 try {
26 if (!category || category.type !== ChannelType.GuildCategory) {
27 await replyUserError(interaction, { type: ErrorTypes.UNKNOWN, message: 'Please select a valid category for the counter channel.' }).catch(logger.error);
28 return;
29 }
30
31 const targetChannelType = channelType === 'voice' ? ChannelType.GuildVoice : ChannelType.GuildText;
32 const baseChannelName = getCounterBaseName(type);
33
34 const counters = await getServerCounters(client, guild.id);
35
36 const duplicateType = counters.find(counter => counter.type === type);
37
38 if (duplicateType) {
39 const duplicateChannel = guild.channels.cache.get(duplicateType.channelId);
40 await replyUserError(interaction, { type: ErrorTypes.UNKNOWN, message: '`A **${getCounterTypeLabel(type)}** counter already exists for this server${duplicateChannel ? ` in ${duplicateChannel}` : \'\'}. Delete it first before creating another.`' }).catch(logger.error);
41 return;
42 }
43
44 const targetChannel = await guild.channels.create({
45 name: baseChannelName,
46 type: targetChannelType,
47 parent: category.id,
48 reason: `Counter channel created by ${interaction.user.tag}`
49 });
50
51 const existingCounter = counters.find(c => c.channelId === targetChannel.id);
52 if (existingCounter) {
53 await replyUserError(interaction, { type: ErrorTypes.UNKNOWN, message: 'A counter already exists for channel **${targetChannel.name}**. Please delete it first or choose a different type.' }).catch(logger.error);
54 return;
55 }
56
57 const newCounter = {
58 id: Date.now().toString(),
59 type: type,
60 channelId: targetChannel.id,
61 guildId: guild.id,
62 createdAt: new Date().toISOString(),
63 enabled: true
64 };

Callers 1

executeFunction · 0.90

Calls 11

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

Tested by

no test coverage detected