Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const { Client, GatewayIntentBits, ApplicationCommandOptionType } = require("discord.js");
- const { createAudioResource, joinVoiceChannel } = require("@discordjs/voice");
- const { IamAuthenticator } = require('ibm-watson/auth');
- const SpeechToTextV1 = require('ibm-watson/speech-to-text/v1');
- const speechToText = new SpeechToTextV1({
- authenticator: new IamAuthenticator({ apikey: process.env.WATSON_SPEECH_TO_TEXT_API_KEY }),
- serviceUrl: process.env.WATSON_SPEECH_TO_TEXT_URL,
- language: 'hu-HU',
- });
- const client = new Client({
- intents: [
- GatewayIntentBits.Guilds,
- GatewayIntentBits.GuildMembers,
- GatewayIntentBits.GuildVoiceStates,
- ],
- });
- let voiceConnection = null;
- let transcribing = false;
- client.on("ready", () => {
- console.log(`Logged in as ${client.user.tag}`);
- // Register slash commands
- client.guilds.cache.forEach((guild) => {
- guild.commands.create({
- name: "pv",
- description: "Perform actions related to voice channels",
- options: [
- {
- name: "action",
- description: "Choose an action",
- type: ApplicationCommandOptionType.String,
- required: true,
- choices: [
- {
- name: "Join",
- value: "join",
- },
- {
- name: "Leave",
- value: "leave",
- },
- {
- name: "Transcribe",
- value: "transcribe",
- },
- {
- name: "Ignore",
- value: "ignore",
- },
- ],
- },
- ],
- });
- });
- });
- client.on("interactionCreate", async (interaction) => {
- if (!interaction.isCommand()) return;
- const { commandName, options } = interaction;
- const action = options.getString("action");
- if (commandName === "pv") {
- if (action === "join") {
- if (!interaction.member.voice.channel) {
- interaction.reply("You are not in a voice channel.");
- return;
- }
- if (voiceConnection) {
- interaction.reply("I am already in a voice channel.");
- return;
- }
- voiceConnection = joinVoiceChannel({
- channelId: interaction.member.voice.channelId,
- guildId: interaction.guildId,
- adapterCreator: interaction.guild.voiceAdapterCreator,
- selfDeaf: false
- });
- // const connectionReady = new Promise(resolve => {
- // const onStatusChange = (oldStatus, newStatus) => {
- // // console.log(oldStatus, newStatus);
- // if (newStatus === 'ready') {
- // resolve();
- // voiceConnection.off('stateChange', onStatusChange);
- // }
- // };
- // voiceConnection.on('stateChange', onStatusChange);
- // });
- // await connectionReady;
- console.log(
- `Joined voice channel ${interaction.member.voice.channel.name}`
- );
- interaction.reply(
- `Joined voice channel ${interaction.member.voice.channel.name}.`
- );
- }
- if (action === "leave") {
- if (!voiceConnection) {
- interaction.reply("I am not in a voice channel.");
- return;
- }
- const channel = await client.channels.fetch(
- voiceConnection.joinConfig.channelId
- );
- voiceConnection.destroy();
- voiceConnection = null;
- transcribing = false;
- console.log(`Left voice channel ${channel.name}`);
- interaction.reply(`Left voice channel ${channel.name}.`);
- }
- if (action === "transcribe") {
- if (!voiceConnection) {
- interaction.reply("I am not in a voice channel.");
- return;
- }
- // console.log("======================= start =======================");
- // console.log("_______ voiceConnection _______");
- // console.log("||||| events |||||");
- // console.log(voiceConnection._events);
- // console.log("----------------------------");
- // console.log("_______ Receiver _______");
- // console.log("||||| main |||||");
- // console.log(voiceConnection.receiver);
- // console.log("-----------");
- // console.log("||||| events |||||");
- // console.log(voiceConnection.receiver._events);
- // console.log("----------------------------");
- // console.log("_______ Speaking _______");
- // console.log("||||| main |||||");
- // console.log(voiceConnection.receiver.speaking);
- // console.log("-----------");
- // console.log("||||| events |||||");
- // console.log(voiceConnection.receiver.speaking._events);
- // console.log("-----------");
- // console.log("||||| users |||||");
- // console.log(voiceConnection.receiver.speaking.users);
- // console.log("-----------");
- // console.log("||||| speaking timeouts |||||");
- // console.log(voiceConnection.receiver.speaking.speakingTimeouts);
- // console.log("======================= end =======================");
- if (transcribing) {
- interaction.reply("I am already transcribing.");
- return;
- }
- transcribing = true;
- const channel = await client.channels.fetch(
- voiceConnection.joinConfig.channelId
- );
- // console.log("--------start--------");
- // console.log("before voiceConnection.on('socketSpeaking', (user, speaking) => {...})");
- client.on('guildMemberSpeaking', (user, speaking) => {
- console.log(user.name, "is talking?", speaking);
- console.log("1");
- })
- voiceConnection.on('Speaking', (user, speaking) => {
- console.log("2");
- })
- voiceConnection.on('ready', () => {
- console.log("voice connection ready");
- voiceConnection.on('socketSpeaking', (user, speaking) => {
- console.log("in voiceConnection.on('socketSpeaking', (user, speaking) => {...})");
- if (speaking) {
- console.log("someone is speaking");
- const audioResource = createAudioResource(user.audioStream, {
- inputType: "opus",
- });
- audioResource.on("data", (data) => {
- console.log("setting audio resource");
- // send data to IBM Watson Speech to Text
- const recognizeParams = {
- audio: data,
- contentType: 'audio/ogg;codecs=opus',
- model: 'hu-HU_BroadbandModel'
- };
- speechToText
- .recognize(recognizeParams)
- .then((transcript) => {
- console.log(`Transcript: ${transcript.result.text}`);
- })
- .catch((err) => {
- console.error("error:", err);
- });
- });
- }
- });
- });
- console.log(`Started transcribing in voice channel ${channel.name}`); // TODO: put this and the reply in the above part when it actually starts it. Also put the variable changes in the start of transcribing.
- interaction.reply(
- `Started transcribing in voice channel ${channel.name}.`
- );
- }
- if (action === "ignore") {
- if (!transcribing) {
- interaction.reply("I am not currently transcribing.");
- return;
- }
- if (!voiceConnection) {
- interaction.reply("I am not in a voice channel.");
- return;
- }
- transcribing = false;
- const channel = await client.channels.fetch(
- voiceConnection.joinConfig.channelId
- );
- console.log(`Stopped transcribing in voice channel ${channel.name}`);
- interaction.reply(
- `Stopped transcribing in voice channel ${channel.name}.`
- );
- }
- }
- });
- client.login(process.env.DISCORD_TOKEN);
Advertisement
Add Comment
Please, Sign In to add comment