blob: 1b9379bad58cbfaa53f5e241646732bb3f6fdc49 [file] [log] [blame]
Skyler Greyda16adf2023-03-05 10:22:12 +00001import { getCommandMentionByName } from "../utils/getCommandDataByName.js";
Skyler Greyf21323a2022-08-13 23:58:22 +01002import type { Guild, User } from "discord.js";
pineafan6de4da52023-03-07 20:43:44 +00003import client from "../utils/client.js";
pineafan63fc5e22022-08-04 22:04:10 +01004import convertCurlyBracketString from "../utils/convertCurlyBracketString.js";
5import singleNotify from "../utils/singleNotify.js";
pineafan708692b2022-07-24 22:16:22 +01006
Skyler Grey75ea9172022-08-06 10:22:23 +01007interface PropSchema {
8 enabled: boolean;
9 name: string;
10}
pineafan708692b2022-07-24 22:16:22 +010011
pineafan6de4da52023-03-07 20:43:44 +000012export async function callback(user: User, guild: Guild) {
13 guild = await client.guilds.fetch(guild.id);
pineafan63fc5e22022-08-04 22:04:10 +010014 const config = await client.database.guilds.read(guild.id);
Skyler Greyc6a93662023-03-08 16:13:39 +000015 Object.entries(config.stats).forEach(
16 ([channel, props]) =>
17 void (async () => {
18 if ((props as PropSchema).enabled) {
19 let string = (props as PropSchema).name;
20 if (!string) return;
21 string = await convertCurlyBracketString(
22 string,
23 user!.id,
24 user!.username,
25 guild!.name,
26 guild!.members
27 );
28 let fetchedChannel;
29 try {
30 fetchedChannel = await guild.channels.fetch(channel);
31 } catch (e) {
32 fetchedChannel = null;
33 }
34 if (!fetchedChannel) {
35 const deleted = config.stats[channel];
36 await client.database.guilds.write(guild.id, null, `stats.${channel}`);
37 return singleNotify(
38 "statsChannelDeleted",
39 guild.id,
40 `One or more of your stats channels have been deleted. You can use ${getCommandMentionByName(
41 "settings/stats"
42 )}.\n` + `The channels name was: ${deleted!.name}`,
43 "Critical"
44 );
45 }
46 try {
47 await fetchedChannel.setName(string.slice(0, 100));
48 } catch (e) {
49 console.error(e);
50 }
51 }
52 })()
53 );
pineafan708692b2022-07-24 22:16:22 +010054}