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