pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame^] | 1 | // @ts-expect-error |
| 2 | import type { HaikuClient } from "jshaiku"; |
| 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 | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame^] | 12 | export async function callback(client: HaikuClient, member: GuildMember) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 13 | const guild = await client.guilds.fetch(member.guild.id); |
| 14 | const config = await client.database.guilds.read(guild.id); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 15 | Object.entries(config.getKey("stats")).forEach(async ([channel, props]) => { |
| 16 | if ((props as PropSchema).enabled) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 17 | let string = (props as PropSchema).name; |
| 18 | if (!string) return; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 19 | string = await convertCurlyBracketString(string, member.id, member.displayName, guild.name, guild.members); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 20 | let fetchedChannel; |
| 21 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 22 | fetchedChannel = await guild.channels.fetch(channel); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 23 | } catch (e) { |
| 24 | fetchedChannel = null; |
| 25 | } |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 26 | if (!fetchedChannel) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 27 | const deleted = config.getKey("stats")[channel]; |
| 28 | console.log(`stats.${channel}`); |
| 29 | console.log(guild.id); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +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", |
| 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" + |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [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 | } |