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