discord-speech-recognition
Version:
Extension for making discord speech recognition bots.
45 lines • 2.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const voice_1 = require("@discordjs/voice");
const events_1 = require("../../events");
/**
* It's a bit hacky solution to check if speech handler has been already attached to connection receiver
* It does it by checking if in the listeners of speaking map exists function with the same name as function
* which handles speech event
* @param connection
* @returns
*/
const isSpeechHandlerAttachedToConnection = (connection) => {
return Boolean(connection.receiver.speaking
.listeners("start")
.find((func) => func.name === "handleSpeechEventOnConnectionReceiver"));
};
/**
* https://github.com/discordjs/discord.js/issues/9185
*
* Workaround from this comment: https://github.com/discordjs/discord.js/issues/9185#issuecomment-1459083216
* @param connection
*/
const fixStoppingAfterOneMinute = (connection) => {
const networkStateChangeHandler = (oldNetworkState, newNetworkState) => {
const newUdp = Reflect.get(newNetworkState, "udp");
clearInterval(newUdp === null || newUdp === void 0 ? void 0 : newUdp.keepAliveInterval);
};
connection.on("stateChange", (oldState, newState) => {
var _a, _b;
(_a = Reflect.get(oldState, "networking")) === null || _a === void 0 ? void 0 : _a.off("stateChange", networkStateChangeHandler);
(_b = Reflect.get(newState, "networking")) === null || _b === void 0 ? void 0 : _b.on("stateChange", networkStateChangeHandler);
});
};
exports.default = (client, speechOptions) => {
client.on("voiceStateUpdate", (_old, newVoiceState) => {
if (!newVoiceState.channel)
return;
const connection = (0, voice_1.getVoiceConnection)(newVoiceState.channel.guild.id, speechOptions.group);
if (connection && !isSpeechHandlerAttachedToConnection(connection)) {
fixStoppingAfterOneMinute(connection);
client.emit(events_1.SpeechEvents.voiceJoin, (0, voice_1.getVoiceConnection)(newVoiceState.channel.guild.id, speechOptions.group));
}
});
};
//# sourceMappingURL=voiceJoin.js.map