blob: 2dee38541d9b76edab8435d055f9b972ce1f6bd2 [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 Grey75ea9172022-08-06 10:22:23 +010017 string = await convertCurlyBracketString(
18 string,
19 member.id,
20 member.displayName,
21 guild.name,
22 guild.members
23 );
pineafan708692b2022-07-24 22:16:22 +010024 let fetchedChannel;
25 try {
pineafan63fc5e22022-08-04 22:04:10 +010026 fetchedChannel = await guild.channels.fetch(channel);
Skyler Grey75ea9172022-08-06 10:22:23 +010027 } catch (e) {
28 fetchedChannel = null;
29 }
pineafan708692b2022-07-24 22:16:22 +010030 if (!fetchedChannel) {
pineafan63fc5e22022-08-04 22:04:10 +010031 const deleted = config.getKey("stats")[channel];
32 console.log(`stats.${channel}`);
33 console.log(guild.id);
Skyler Grey75ea9172022-08-06 10:22:23 +010034 await client.database.guilds.write(
35 guild.id,
36 null,
37 `stats.${channel}`
38 );
pineafan708692b2022-07-24 22:16:22 +010039 return singleNotify(
40 "statsChannelDeleted",
41 guild.id,
pineafane23c4ec2022-07-27 21:56:27 +010042 "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 +010043 `The channels name was: ${deleted.name}`,
pineafan708692b2022-07-24 22:16:22 +010044 "Critical"
pineafan63fc5e22022-08-04 22:04:10 +010045 );
pineafan708692b2022-07-24 22:16:22 +010046 }
47 try {
pineafan63fc5e22022-08-04 22:04:10 +010048 await fetchedChannel.setName(string.slice(0, 100));
pineafan708692b2022-07-24 22:16:22 +010049 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +010050 console.error(e);
pineafan708692b2022-07-24 22:16:22 +010051 }
52 }
53 });
54}