blob: 850e826ebed42f612c40e5169cfc335e7b3abe67 [file] [log] [blame]
Skyler Greyf21323a2022-08-13 23:58:22 +01001import type { Guild, User } from "discord.js";
pineafan3a02ea32022-08-11 21:35:04 +01002// @ts-expect-error
3import type { HaikuClient } from "jshaiku";
4import type { GuildMember } from "discord.js";
pineafan63fc5e22022-08-04 22:04:10 +01005import convertCurlyBracketString from "../utils/convertCurlyBracketString.js";
6import singleNotify from "../utils/singleNotify.js";
pineafan708692b2022-07-24 22:16:22 +01007
Skyler Grey75ea9172022-08-06 10:22:23 +01008interface PropSchema {
9 enabled: boolean;
10 name: string;
11}
pineafan708692b2022-07-24 22:16:22 +010012
pineafan0f5cc782022-08-12 21:55:42 +010013export async function callback(client: HaikuClient, member?: GuildMember, guild?: Guild, user?: User) {
14 if (!member && !guild) return;
15 guild = await client.guilds.fetch(member ? member.guild.id : guild!.id);
16 if (!guild) return;
Skyler Greyf21323a2022-08-13 23:58:22 +010017 user = user ?? member!.user;
pineafan63fc5e22022-08-04 22:04:10 +010018 const config = await client.database.guilds.read(guild.id);
pineafan708692b2022-07-24 22:16:22 +010019 Object.entries(config.getKey("stats")).forEach(async ([channel, props]) => {
20 if ((props as PropSchema).enabled) {
pineafan63fc5e22022-08-04 22:04:10 +010021 let string = (props as PropSchema).name;
22 if (!string) return;
pineafan0f5cc782022-08-12 21:55:42 +010023 string = await convertCurlyBracketString(string, user!.id, user!.username, guild!.name, guild!.members);
pineafan708692b2022-07-24 22:16:22 +010024 let fetchedChannel;
25 try {
pineafan0f5cc782022-08-12 21:55:42 +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];
pineafan0f5cc782022-08-12 21:55:42 +010032 await client.database.guilds.write(guild!.id, null, `stats.${channel}`);
pineafan708692b2022-07-24 22:16:22 +010033 return singleNotify(
34 "statsChannelDeleted",
pineafan0f5cc782022-08-12 21:55:42 +010035 guild!.id,
pineafane23c4ec2022-07-27 21:56:27 +010036 "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 +010037 `The channels name was: ${deleted.name}`,
pineafan708692b2022-07-24 22:16:22 +010038 "Critical"
pineafan63fc5e22022-08-04 22:04:10 +010039 );
pineafan708692b2022-07-24 22:16:22 +010040 }
41 try {
pineafan63fc5e22022-08-04 22:04:10 +010042 await fetchedChannel.setName(string.slice(0, 100));
pineafan708692b2022-07-24 22:16:22 +010043 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +010044 console.error(e);
pineafan708692b2022-07-24 22:16:22 +010045 }
46 }
47 });
48}