blob: f2650a8ae34eba8fcbb6b364271f90d31403efb2 [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 Greyf4f21c42023-03-08 14:36:29 +000015 Object.entries(config.stats).forEach(([channel, props]) => void (async () => {
pineafan708692b2022-07-24 22:16:22 +010016 if ((props as PropSchema).enabled) {
pineafan63fc5e22022-08-04 22:04:10 +010017 let string = (props as PropSchema).name;
18 if (!string) return;
pineafan0f5cc782022-08-12 21:55:42 +010019 string = await convertCurlyBracketString(string, user!.id, user!.username, guild!.name, guild!.members);
pineafan708692b2022-07-24 22:16:22 +010020 let fetchedChannel;
21 try {
pineafan6de4da52023-03-07 20:43:44 +000022 fetchedChannel = await guild.channels.fetch(channel);
Skyler Grey75ea9172022-08-06 10:22:23 +010023 } catch (e) {
24 fetchedChannel = null;
25 }
pineafan708692b2022-07-24 22:16:22 +010026 if (!fetchedChannel) {
PineaFana34d04b2023-01-03 22:05:42 +000027 const deleted = config.stats[channel];
pineafan6de4da52023-03-07 20:43:44 +000028 await client.database.guilds.write(guild.id, null, `stats.${channel}`);
pineafan708692b2022-07-24 22:16:22 +010029 return singleNotify(
30 "statsChannelDeleted",
pineafan6de4da52023-03-07 20:43:44 +000031 guild.id,
Skyler Greyda16adf2023-03-05 10:22:12 +000032 `One or more of your stats channels have been deleted. You can use ${getCommandMentionByName(
33 "settings/stats"
34 )}.\n` + `The channels name was: ${deleted!.name}`,
pineafan708692b2022-07-24 22:16:22 +010035 "Critical"
pineafan63fc5e22022-08-04 22:04:10 +010036 );
pineafan708692b2022-07-24 22:16:22 +010037 }
38 try {
pineafan63fc5e22022-08-04 22:04:10 +010039 await fetchedChannel.setName(string.slice(0, 100));
pineafan708692b2022-07-24 22:16:22 +010040 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +010041 console.error(e);
pineafan708692b2022-07-24 22:16:22 +010042 }
43 }
Skyler Greyf4f21c42023-03-08 14:36:29 +000044 })());
pineafan708692b2022-07-24 22:16:22 +010045}