pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import convertCurlyBracketString from "../utils/convertCurlyBracketString.js"; |
| 2 | import singleNotify from "../utils/singleNotify.js"; |
| 3 | import client from "../utils/client.js"; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 4 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 5 | interface PropSchema { |
| 6 | enabled: boolean; |
| 7 | name: string; |
| 8 | } |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 9 | |
| 10 | export async function callback(_, member) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 11 | const guild = await client.guilds.fetch(member.guild.id); |
| 12 | const config = await client.database.guilds.read(guild.id); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 13 | Object.entries(config.getKey("stats")).forEach(async ([channel, props]) => { |
| 14 | if ((props as PropSchema).enabled) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 15 | let string = (props as PropSchema).name; |
| 16 | if (!string) return; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 17 | string = await convertCurlyBracketString(string, member.id, member.displayName, guild.name, guild.members); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 18 | let fetchedChannel; |
| 19 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 20 | fetchedChannel = await guild.channels.fetch(channel); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 21 | } catch (e) { |
| 22 | fetchedChannel = null; |
| 23 | } |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 24 | if (!fetchedChannel) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 25 | const deleted = config.getKey("stats")[channel]; |
| 26 | console.log(`stats.${channel}`); |
| 27 | console.log(guild.id); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [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", |
| 31 | guild.id, |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 32 | "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] | 33 | `The channels name was: ${deleted.name}`, |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 34 | "Critical" |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 35 | ); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 36 | } |
| 37 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 38 | await fetchedChannel.setName(string.slice(0, 100)); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 39 | } catch (e) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 40 | console.error(e); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | }); |
| 44 | } |