Skip to content

Commit b5f0f5c

Browse files
committed
ignore vc updates if no channelId change
1 parent d89d9cc commit b5f0f5c

File tree

3 files changed

+86
-38
lines changed

3 files changed

+86
-38
lines changed

config-loader.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

config.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { readFileSync } from "fs";
2+
import { safeJsonParse } from "./utils.js";
3+
import isEmpty from "lodash.isempty";
4+
import isEqual from "lodash.isequal";
5+
6+
const schema = Object.freeze({
7+
token: typeof String,
8+
channels: typeof Array,
9+
members: typeof Array
10+
});
11+
12+
const load = (configPath) => {
13+
const configFileContent = readFileSync(configPath, "utf-8");
14+
const configJson = safeJsonParse(configFileContent);
15+
16+
if (isEmpty(configJson)) {
17+
throw `config.json is malformed: ${configFileContent}`;
18+
}
19+
20+
Object.entries(schema).forEach((property, type) => {
21+
if (!configJson.hasOwnProperty(property) && isEqual(typeof configJson[property], type)) {
22+
throw `[config.json] Missing or malformed property: ${property}`
23+
}
24+
})
25+
26+
return configJson;
27+
}
28+
29+
export default {
30+
load
31+
}

index.js

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,78 @@
77
* buy me a beer in return.
88
* ------------------------------------------------------------
99
*/
10-
import { loadConfig } from "./config-loader.js";
1110
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")
1320

1421
const client = new Client({
1522
intents: [
1623
GatewayIntentBits.Guilds,
1724
GatewayIntentBits.GuildVoiceStates
18-
]
25+
],
26+
sweepers: {
27+
...Options.DefaultSweeperSettings,
28+
messages: {
29+
interval: 1_800,
30+
lifetime: 300,
31+
maxSize: 1
32+
}
33+
}
1934
});
2035

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);
2639

2740
client.on("ready", () => {
2841
console.log(`Logged in as ${client.user.tag}!`)
2942
});
3043

3144
client.on("voiceStateUpdate", async (oldState, newState) => {
32-
const { member, channel } = newState;
45+
if (!newState.channel) return;
3346

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)
3550

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+
];
3963

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+
)
4382
}
4483
});
4584

0 commit comments

Comments
 (0)