blob: a331537aac60444c80893f278155a232ad56c9a1 [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import convertCurlyBracketString from "../utils/convertCurlyBracketString.js";
2import singleNotify from "../utils/singleNotify.js";
3import client from "../utils/client.js";
pineafan708692b2022-07-24 22:16:22 +01004
Skyler Grey75ea9172022-08-06 10:22:23 +01005interface PropSchema {
6 enabled: boolean;
7 name: string;
8}
pineafan708692b2022-07-24 22:16:22 +01009
10export async function callback(_, member) {
pineafan63fc5e22022-08-04 22:04:10 +010011 const guild = await client.guilds.fetch(member.guild.id);
12 const config = await client.database.guilds.read(guild.id);
pineafan708692b2022-07-24 22:16:22 +010013 Object.entries(config.getKey("stats")).forEach(async ([channel, props]) => {
14 if ((props as PropSchema).enabled) {
pineafan63fc5e22022-08-04 22:04:10 +010015 let string = (props as PropSchema).name;
16 if (!string) return;
Skyler Grey11236ba2022-08-08 21:13:33 +010017 string = await convertCurlyBracketString(string, member.id, member.displayName, guild.name, guild.members);
pineafan708692b2022-07-24 22:16:22 +010018 let fetchedChannel;
19 try {
pineafan63fc5e22022-08-04 22:04:10 +010020 fetchedChannel = await guild.channels.fetch(channel);
Skyler Grey75ea9172022-08-06 10:22:23 +010021 } catch (e) {
22 fetchedChannel = null;
23 }
pineafan708692b2022-07-24 22:16:22 +010024 if (!fetchedChannel) {
pineafan63fc5e22022-08-04 22:04:10 +010025 const deleted = config.getKey("stats")[channel];
26 console.log(`stats.${channel}`);
27 console.log(guild.id);
Skyler Grey11236ba2022-08-08 21:13:33 +010028 await client.database.guilds.write(guild.id, null, `stats.${channel}`);
pineafan708692b2022-07-24 22:16:22 +010029 return singleNotify(
30 "statsChannelDeleted",
31 guild.id,
pineafane23c4ec2022-07-27 21:56:27 +010032 "One or more of your stats channels have been deleted. Please use `/settings stats` if you wish to add the channel again.\n" +
Skyler Grey75ea9172022-08-06 10:22:23 +010033 `The channels name was: ${deleted.name}`,
pineafan708692b2022-07-24 22:16:22 +010034 "Critical"
pineafan63fc5e22022-08-04 22:04:10 +010035 );
pineafan708692b2022-07-24 22:16:22 +010036 }
37 try {
pineafan63fc5e22022-08-04 22:04:10 +010038 await fetchedChannel.setName(string.slice(0, 100));
pineafan708692b2022-07-24 22:16:22 +010039 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +010040 console.error(e);
pineafan708692b2022-07-24 22:16:22 +010041 }
42 }
43 });
44}