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 | c6a9366 | 2023-03-08 16:13:39 +0000 | [diff] [blame^] | 15 | Object.entries(config.stats).forEach( |
| 16 | ([channel, props]) => |
| 17 | void (async () => { |
| 18 | if ((props as PropSchema).enabled) { |
| 19 | let string = (props as PropSchema).name; |
| 20 | if (!string) return; |
| 21 | string = await convertCurlyBracketString( |
| 22 | string, |
| 23 | user!.id, |
| 24 | user!.username, |
| 25 | guild!.name, |
| 26 | guild!.members |
| 27 | ); |
| 28 | let fetchedChannel; |
| 29 | try { |
| 30 | fetchedChannel = await guild.channels.fetch(channel); |
| 31 | } catch (e) { |
| 32 | fetchedChannel = null; |
| 33 | } |
| 34 | if (!fetchedChannel) { |
| 35 | const deleted = config.stats[channel]; |
| 36 | await client.database.guilds.write(guild.id, null, `stats.${channel}`); |
| 37 | return singleNotify( |
| 38 | "statsChannelDeleted", |
| 39 | guild.id, |
| 40 | `One or more of your stats channels have been deleted. You can use ${getCommandMentionByName( |
| 41 | "settings/stats" |
| 42 | )}.\n` + `The channels name was: ${deleted!.name}`, |
| 43 | "Critical" |
| 44 | ); |
| 45 | } |
| 46 | try { |
| 47 | await fetchedChannel.setName(string.slice(0, 100)); |
| 48 | } catch (e) { |
| 49 | console.error(e); |
| 50 | } |
| 51 | } |
| 52 | })() |
| 53 | ); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 54 | } |