blob: 2e124294c92f7f58ea481fe7960f698e9d547b36 [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);
Skyler Greyf21323a2022-08-13 23:58:22 +010015 user = user ?? member!.user;
pineafan63fc5e22022-08-04 22:04:10 +010016 const config = await client.database.guilds.read(guild.id);
PineaFana34d04b2023-01-03 22:05:42 +000017 Object.entries(config.stats).forEach(async ([channel, props]) => {
pineafan708692b2022-07-24 22:16:22 +010018 if ((props as PropSchema).enabled) {
pineafan63fc5e22022-08-04 22:04:10 +010019 let string = (props as PropSchema).name;
20 if (!string) return;
pineafan0f5cc782022-08-12 21:55:42 +010021 string = await convertCurlyBracketString(string, user!.id, user!.username, guild!.name, guild!.members);
pineafan708692b2022-07-24 22:16:22 +010022 let fetchedChannel;
23 try {
pineafan0f5cc782022-08-12 21:55:42 +010024 fetchedChannel = await guild!.channels.fetch(channel);
Skyler Grey75ea9172022-08-06 10:22:23 +010025 } catch (e) {
26 fetchedChannel = null;
27 }
pineafan708692b2022-07-24 22:16:22 +010028 if (!fetchedChannel) {
PineaFana34d04b2023-01-03 22:05:42 +000029 const deleted = config.stats[channel];
pineafan0f5cc782022-08-12 21:55:42 +010030 await client.database.guilds.write(guild!.id, null, `stats.${channel}`);
pineafan708692b2022-07-24 22:16:22 +010031 return singleNotify(
32 "statsChannelDeleted",
pineafan0f5cc782022-08-12 21:55:42 +010033 guild!.id,
pineafane23c4ec2022-07-27 21:56:27 +010034 "One or more of your stats channels have been deleted. Please use `/settings stats` if you wish to add the channel again.\n" +
PineaFana34d04b2023-01-03 22:05:42 +000035 `The channels name was: ${deleted!.name}`,
pineafan708692b2022-07-24 22:16:22 +010036 "Critical"
pineafan63fc5e22022-08-04 22:04:10 +010037 );
pineafan708692b2022-07-24 22:16:22 +010038 }
39 try {
pineafan63fc5e22022-08-04 22:04:10 +010040 await fetchedChannel.setName(string.slice(0, 100));
pineafan708692b2022-07-24 22:16:22 +010041 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +010042 console.error(e);
pineafan708692b2022-07-24 22:16:22 +010043 }
44 }
45 });
46}