blob: 6c601f730dc5859636c0b6b86dbcaa4280d7036e [file] [log] [blame]
Skyler Greyda16adf2023-03-05 10:22:12 +00001import { getCommandMentionByName } from "../utils/getCommandDataByName.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,
Skyler Greyda16adf2023-03-05 10:22:12 +000035 `One or more of your stats channels have been deleted. You can use ${getCommandMentionByName(
36 "settings/stats"
37 )}.\n` + `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}