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 | |
| 5 | interface PropSchema { enabled: boolean, name: string } |
| 6 | |
| 7 | export async function callback(_, member) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 8 | const guild = await client.guilds.fetch(member.guild.id); |
| 9 | const config = await client.database.guilds.read(guild.id); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 10 | Object.entries(config.getKey("stats")).forEach(async ([channel, props]) => { |
| 11 | if ((props as PropSchema).enabled) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 12 | let string = (props as PropSchema).name; |
| 13 | if (!string) return; |
| 14 | string = await convertCurlyBracketString(string, member.id, member.displayName, guild.name, guild.members); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 15 | let fetchedChannel; |
| 16 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 17 | fetchedChannel = await guild.channels.fetch(channel); |
| 18 | } catch (e) { fetchedChannel = null; } |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 19 | if (!fetchedChannel) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 20 | const deleted = config.getKey("stats")[channel]; |
| 21 | console.log(`stats.${channel}`); |
| 22 | console.log(guild.id); |
| 23 | await client.database.guilds.write(guild.id, null, `stats.${channel}`); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 24 | return singleNotify( |
| 25 | "statsChannelDeleted", |
| 26 | guild.id, |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 27 | "One or more of your stats channels have been deleted. Please use `/settings stats` if you wish to add the channel again.\n" + |
| 28 | `The channels name was: ${deleted.name}`, |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 29 | "Critical" |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 30 | ); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 31 | } |
| 32 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 33 | await fetchedChannel.setName(string.slice(0, 100)); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 34 | } catch (e) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 35 | console.error(e); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 36 | } |
| 37 | } |
| 38 | }); |
| 39 | } |