Skyler Grey | f21323a | 2022-08-13 23:58:22 +0100 | [diff] [blame] | 1 | import type { Guild, User } from "discord.js"; |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame^] | 2 | import type { HaikuClient } from "../utils/haiku/index.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 | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 12 | export async function callback(client: HaikuClient, member?: GuildMember, guild?: Guild, user?: User) { |
| 13 | if (!member && !guild) return; |
| 14 | guild = await client.guilds.fetch(member ? member.guild.id : guild!.id); |
| 15 | if (!guild) return; |
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 | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 18 | Object.entries(config.getKey("stats")).forEach(async ([channel, props]) => { |
| 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 | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 30 | const deleted = config.getKey("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, |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 35 | "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] | 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 | } |