Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [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 | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 3 | import client from "../utils/client.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 | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 12 | export async function callback(user: User, guild: Guild) { |
| 13 | guild = await client.guilds.fetch(guild.id); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 14 | const config = await client.database.guilds.read(guild.id); |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 15 | Object.entries(config.stats).forEach(([channel, props]) => void (async () => { |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 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; |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 19 | string = await convertCurlyBracketString(string, user!.id, user!.username, guild!.name, guild!.members); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 20 | let fetchedChannel; |
| 21 | try { |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [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 | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 27 | const deleted = config.stats[channel]; |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 28 | await client.database.guilds.write(guild.id, null, `stats.${channel}`); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 29 | return singleNotify( |
| 30 | "statsChannelDeleted", |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 31 | guild.id, |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 32 | `One or more of your stats channels have been deleted. You can use ${getCommandMentionByName( |
| 33 | "settings/stats" |
| 34 | )}.\n` + `The channels name was: ${deleted!.name}`, |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 35 | "Critical" |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 36 | ); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 37 | } |
| 38 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 39 | await fetchedChannel.setName(string.slice(0, 100)); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 40 | } catch (e) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 41 | console.error(e); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 42 | } |
| 43 | } |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 44 | })()); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 45 | } |