blob: cd1c8ef1d2fc9f33936ef087a2b998c6523b1b97 [file] [log] [blame]
Skyler Greyf21323a2022-08-13 23:58:22 +01001import type { Guild, User } from "discord.js";
PineaFan64486c42022-12-28 09:21:04 +00002import type { HaikuClient } from "../utils/haiku/index.js";
pineafan3a02ea32022-08-11 21:35:04 +01003import 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
pineafan0f5cc782022-08-12 21:55:42 +010012export async function callback(client: HaikuClient, member?: GuildMember, guild?: Guild, user?: User) {
13 if (!member && !guild) return;
14 guild = await client.guilds.fetch(member ? member.guild.id : guild!.id);
15 if (!guild) return;
Skyler Greyf21323a2022-08-13 23:58:22 +010016 user = user ?? member!.user;
pineafan63fc5e22022-08-04 22:04:10 +010017 const config = await client.database.guilds.read(guild.id);
pineafan708692b2022-07-24 22:16:22 +010018 Object.entries(config.getKey("stats")).forEach(async ([channel, props]) => {
19 if ((props as PropSchema).enabled) {
pineafan63fc5e22022-08-04 22:04:10 +010020 let string = (props as PropSchema).name;
21 if (!string) return;
pineafan0f5cc782022-08-12 21:55:42 +010022 string = await convertCurlyBracketString(string, user!.id, user!.username, guild!.name, guild!.members);
pineafan708692b2022-07-24 22:16:22 +010023 let fetchedChannel;
24 try {
pineafan0f5cc782022-08-12 21:55:42 +010025 fetchedChannel = await guild!.channels.fetch(channel);
Skyler Grey75ea9172022-08-06 10:22:23 +010026 } catch (e) {
27 fetchedChannel = null;
28 }
pineafan708692b2022-07-24 22:16:22 +010029 if (!fetchedChannel) {
pineafan63fc5e22022-08-04 22:04:10 +010030 const deleted = config.getKey("stats")[channel];
pineafan0f5cc782022-08-12 21:55:42 +010031 await client.database.guilds.write(guild!.id, null, `stats.${channel}`);
pineafan708692b2022-07-24 22:16:22 +010032 return singleNotify(
33 "statsChannelDeleted",
pineafan0f5cc782022-08-12 21:55:42 +010034 guild!.id,
pineafane23c4ec2022-07-27 21:56:27 +010035 "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 +010036 `The channels name was: ${deleted.name}`,
pineafan708692b2022-07-24 22:16:22 +010037 "Critical"
pineafan63fc5e22022-08-04 22:04:10 +010038 );
pineafan708692b2022-07-24 22:16:22 +010039 }
40 try {
pineafan63fc5e22022-08-04 22:04:10 +010041 await fetchedChannel.setName(string.slice(0, 100));
pineafan708692b2022-07-24 22:16:22 +010042 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +010043 console.error(e);
pineafan708692b2022-07-24 22:16:22 +010044 }
45 }
46 });
47}