blob: d8072672e275252b9f9f03331a2e7cd29e39d620 [file] [log] [blame]
Skyler Greyf21323a2022-08-13 23:58:22 +01001import type { Guild, User } from "discord.js";
PineaFan752af462022-12-31 21:59:38 +00002import type { NucleusClient } from "../utils/client.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
PineaFan752af462022-12-31 21:59:38 +000012export async function callback(client: NucleusClient, member?: GuildMember, guild?: Guild, user?: User) {
pineafan0f5cc782022-08-12 21:55:42 +010013 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}