blob: 644a75f664ed279e2d3ac0cf8a7ce0c21c508d21 [file] [log] [blame]
pineafan3a02ea32022-08-11 21:35:04 +01001// @ts-expect-error
2import type { HaikuClient } from "jshaiku";
3import type { GuildMember } from "discord.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
pineafan3a02ea32022-08-11 21:35:04 +010012export async function callback(client: HaikuClient, member: GuildMember) {
pineafan63fc5e22022-08-04 22:04:10 +010013 const guild = await client.guilds.fetch(member.guild.id);
14 const config = await client.database.guilds.read(guild.id);
pineafan708692b2022-07-24 22:16:22 +010015 Object.entries(config.getKey("stats")).forEach(async ([channel, props]) => {
16 if ((props as PropSchema).enabled) {
pineafan63fc5e22022-08-04 22:04:10 +010017 let string = (props as PropSchema).name;
18 if (!string) return;
Skyler Grey11236ba2022-08-08 21:13:33 +010019 string = await convertCurlyBracketString(string, member.id, member.displayName, guild.name, guild.members);
pineafan708692b2022-07-24 22:16:22 +010020 let fetchedChannel;
21 try {
pineafan63fc5e22022-08-04 22:04:10 +010022 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) {
pineafan63fc5e22022-08-04 22:04:10 +010027 const deleted = config.getKey("stats")[channel];
28 console.log(`stats.${channel}`);
29 console.log(guild.id);
Skyler Grey11236ba2022-08-08 21:13:33 +010030 await client.database.guilds.write(guild.id, null, `stats.${channel}`);
pineafan708692b2022-07-24 22:16:22 +010031 return singleNotify(
32 "statsChannelDeleted",
33 guild.id,
pineafane23c4ec2022-07-27 21:56:27 +010034 "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 +010035 `The channels name was: ${deleted.name}`,
pineafan708692b2022-07-24 22:16:22 +010036 "Critical"
pineafan63fc5e22022-08-04 22:04:10 +010037 );
pineafan708692b2022-07-24 22:16:22 +010038 }
39 try {
pineafan63fc5e22022-08-04 22:04:10 +010040 await fetchedChannel.setName(string.slice(0, 100));
pineafan708692b2022-07-24 22:16:22 +010041 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +010042 console.error(e);
pineafan708692b2022-07-24 22:16:22 +010043 }
44 }
45 });
46}