blob: db705d9cdb812ee898995857c94e2f978bc17399 [file] [log] [blame]
PineaFane6ba7882023-01-18 20:41:16 +00001import { getCommandMentionByName } from '../utils/getCommandMentionByName.js';
Skyler Greyf21323a2022-08-13 23:58:22 +01002import type { Guild, User } from "discord.js";
PineaFan752af462022-12-31 21:59:38 +00003import type { NucleusClient } from "../utils/client.js";
pineafan3a02ea32022-08-11 21:35:04 +01004import 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
PineaFan752af462022-12-31 21:59:38 +000013export async function callback(client: NucleusClient, member?: GuildMember, guild?: Guild, user?: User) {
pineafan0f5cc782022-08-12 21:55:42 +010014 if (!member && !guild) return;
15 guild = await client.guilds.fetch(member ? member.guild.id : guild!.id);
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);
PineaFana34d04b2023-01-03 22:05:42 +000018 Object.entries(config.stats).forEach(async ([channel, props]) => {
pineafan708692b2022-07-24 22:16:22 +010019 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) {
PineaFana34d04b2023-01-03 22:05:42 +000030 const deleted = config.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,
PineaFane6ba7882023-01-18 20:41:16 +000035 `One or more of your stats channels have been deleted. You can use ${await getCommandMentionByName("settings/stats")}.\n` +
PineaFana34d04b2023-01-03 22:05:42 +000036 `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}