blob: 7301a60c186e2f719cbc04a14b65d1186abc9a50 [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
5interface PropSchema { enabled: boolean, name: string }
6
7export async function callback(_, member) {
pineafan63fc5e22022-08-04 22:04:10 +01008 const guild = await client.guilds.fetch(member.guild.id);
9 const config = await client.database.guilds.read(guild.id);
pineafan708692b2022-07-24 22:16:22 +010010 Object.entries(config.getKey("stats")).forEach(async ([channel, props]) => {
11 if ((props as PropSchema).enabled) {
pineafan63fc5e22022-08-04 22:04:10 +010012 let string = (props as PropSchema).name;
13 if (!string) return;
14 string = await convertCurlyBracketString(string, member.id, member.displayName, guild.name, guild.members);
pineafan708692b2022-07-24 22:16:22 +010015 let fetchedChannel;
16 try {
pineafan63fc5e22022-08-04 22:04:10 +010017 fetchedChannel = await guild.channels.fetch(channel);
18 } catch (e) { fetchedChannel = null; }
pineafan708692b2022-07-24 22:16:22 +010019 if (!fetchedChannel) {
pineafan63fc5e22022-08-04 22:04:10 +010020 const deleted = config.getKey("stats")[channel];
21 console.log(`stats.${channel}`);
22 console.log(guild.id);
23 await client.database.guilds.write(guild.id, null, `stats.${channel}`);
pineafan708692b2022-07-24 22:16:22 +010024 return singleNotify(
25 "statsChannelDeleted",
26 guild.id,
pineafane23c4ec2022-07-27 21:56:27 +010027 "One or more of your stats channels have been deleted. Please use `/settings stats` if you wish to add the channel again.\n" +
28 `The channels name was: ${deleted.name}`,
pineafan708692b2022-07-24 22:16:22 +010029 "Critical"
pineafan63fc5e22022-08-04 22:04:10 +010030 );
pineafan708692b2022-07-24 22:16:22 +010031 }
32 try {
pineafan63fc5e22022-08-04 22:04:10 +010033 await fetchedChannel.setName(string.slice(0, 100));
pineafan708692b2022-07-24 22:16:22 +010034 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +010035 console.error(e);
pineafan708692b2022-07-24 22:16:22 +010036 }
37 }
38 });
39}