|
7 | 7 | * buy me a beer in return. |
8 | 8 | * ------------------------------------------------------------ |
9 | 9 | */ |
10 | | -import { loadConfig } from "./config-loader.js"; |
11 | 10 | import isEqual from "lodash.isequal"; |
12 | | -import { Client, GatewayIntentBits, ChannelType } from"discord.js"; |
| 11 | +import config from "./config.js"; |
| 12 | +import {Client, Options, GatewayIntentBits, ChannelType} from "discord.js"; |
| 13 | +import isEmpty from "lodash.isempty"; |
| 14 | + |
| 15 | +/** |
| 16 | + * @property token <string> Client token |
| 17 | + * @property channelName <string> Name of the Discord channel receiving voice chat notifications |
| 18 | + */ |
| 19 | +const {token, channels, members} = config.load("./config.json") |
13 | 20 |
|
14 | 21 | const client = new Client({ |
15 | 22 | intents: [ |
16 | 23 | GatewayIntentBits.Guilds, |
17 | 24 | GatewayIntentBits.GuildVoiceStates |
18 | | - ] |
| 25 | + ], |
| 26 | + sweepers: { |
| 27 | + ...Options.DefaultSweeperSettings, |
| 28 | + messages: { |
| 29 | + interval: 1_800, |
| 30 | + lifetime: 300, |
| 31 | + maxSize: 1 |
| 32 | + } |
| 33 | + } |
19 | 34 | }); |
20 | 35 |
|
21 | | -/** |
22 | | - * @property token <string> Client token |
23 | | - * @property channelName <string> Name of the Discord channel receiving voice chat notifications |
24 | | - */ |
25 | | -const { token, channelName } = loadConfig("./config.json") |
| 36 | +const handleError = (e) => console.error(e); |
| 37 | + |
| 38 | +client.on("error", handleError); |
26 | 39 |
|
27 | 40 | client.on("ready", () => { |
28 | 41 | console.log(`Logged in as ${client.user.tag}!`) |
29 | 42 | }); |
30 | 43 |
|
31 | 44 | client.on("voiceStateUpdate", async (oldState, newState) => { |
32 | | - const { member, channel } = newState; |
| 45 | + if (!newState.channel) return; |
33 | 46 |
|
34 | | - if (!channel) return; |
| 47 | + const isUserUpdate = !newState.member.user.bot |
| 48 | + const isChannelChange = !isEqual(oldState.channelId, newState.channelId) |
| 49 | + const isVoiceChannel = isEqual(newState.channel.type, ChannelType.GuildVoice) |
35 | 50 |
|
36 | | - const textChannel = member.guild.channels.cache.find((channel) => |
37 | | - isEqual(channel.name, channelName) && isEqual(channel.type, ChannelType.GuildText) |
38 | | - ); |
| 51 | + if (isUserUpdate && isVoiceChannel && isChannelChange) { |
| 52 | + const notificationChannels = [ |
| 53 | + // config.members channels |
| 54 | + ...(!isEmpty(members) ? await Promise.all(members.map((id) => client.users.fetch(id).catch(handleError))) : []), |
| 55 | + // config.channels channels |
| 56 | + ...(!isEmpty(channels) ? await Promise.all( |
| 57 | + channels.map((channelName) => newState.member.guild.channels.cache.find(({name, type}) => |
| 58 | + isEqual(name, channelName) && isEqual(type, ChannelType.GuildText) |
| 59 | + ) |
| 60 | + ) |
| 61 | + ) : []) |
| 62 | + ]; |
39 | 63 |
|
40 | | - if (textChannel && isEqual(channel.type, ChannelType.GuildVoice)) { |
41 | | - const message = await textChannel.send(`${channel}:`); |
42 | | - await message.edit(`${channel}: ${member} has joined`) |
| 64 | + await Promise.all(notificationChannels.map((notificationChannel) => |
| 65 | + new Promise(async (resolve, reject) => { |
| 66 | + let message = null; |
| 67 | + try { |
| 68 | + message = await notificationChannel.send(`${newState.channel}:`); |
| 69 | + await message.edit(`${newState.channel}: ${newState.member} has joined`); |
| 70 | + } catch (e) { |
| 71 | + await message?.delete() |
| 72 | + .then(({author}) => console.log( |
| 73 | + `Deleted failed message attempt from ${author.username} to ${notificationChannel.name}` |
| 74 | + )) |
| 75 | + .catch((e) => reject(e)); |
| 76 | + } finally { |
| 77 | + resolve() |
| 78 | + } |
| 79 | + }) |
| 80 | + ) |
| 81 | + ) |
43 | 82 | } |
44 | 83 | }); |
45 | 84 |
|
|
0 commit comments