Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 1 | import { getCommandMentionByName } from '../utils/getCommandDataByName.js'; |
Skyler Grey | f21323a | 2022-08-13 23:58:22 +0100 | [diff] [blame] | 2 | import type { Guild, User } from "discord.js"; |
PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame] | 3 | import type { NucleusClient } from "../utils/client.js"; |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 4 | import type { GuildMember } from "discord.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 5 | import convertCurlyBracketString from "../utils/convertCurlyBracketString.js"; |
| 6 | import singleNotify from "../utils/singleNotify.js"; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 7 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 8 | interface PropSchema { |
| 9 | enabled: boolean; |
| 10 | name: string; |
| 11 | } |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 12 | |
PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame] | 13 | export async function callback(client: NucleusClient, member?: GuildMember, guild?: Guild, user?: User) { |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 14 | if (!member && !guild) return; |
| 15 | guild = await client.guilds.fetch(member ? member.guild.id : guild!.id); |
Skyler Grey | f21323a | 2022-08-13 23:58:22 +0100 | [diff] [blame] | 16 | user = user ?? member!.user; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 17 | const config = await client.database.guilds.read(guild.id); |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 18 | Object.entries(config.stats).forEach(async ([channel, props]) => { |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 19 | if ((props as PropSchema).enabled) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 20 | let string = (props as PropSchema).name; |
| 21 | if (!string) return; |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 22 | string = await convertCurlyBracketString(string, user!.id, user!.username, guild!.name, guild!.members); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 23 | let fetchedChannel; |
| 24 | try { |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 25 | fetchedChannel = await guild!.channels.fetch(channel); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 26 | } catch (e) { |
| 27 | fetchedChannel = null; |
| 28 | } |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 29 | if (!fetchedChannel) { |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 30 | const deleted = config.stats[channel]; |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 31 | await client.database.guilds.write(guild!.id, null, `stats.${channel}`); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 32 | return singleNotify( |
| 33 | "statsChannelDeleted", |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 34 | guild!.id, |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 35 | `One or more of your stats channels have been deleted. You can use ${getCommandMentionByName("settings/stats")}.\n` + |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 36 | `The channels name was: ${deleted!.name}`, |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 37 | "Critical" |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 38 | ); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 39 | } |
| 40 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 41 | await fetchedChannel.setName(string.slice(0, 100)); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 42 | } catch (e) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 43 | console.error(e); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | }); |
| 47 | } |