Guest User

DiscordBot help

a guest
Jul 14th, 2023
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 8.09 KB | Source Code | 0 0
  1. const { Client, GatewayIntentBits, ApplicationCommandOptionType } = require("discord.js");
  2. const { createAudioResource, joinVoiceChannel } = require("@discordjs/voice");
  3. const { IamAuthenticator } = require('ibm-watson/auth');
  4. const SpeechToTextV1 = require('ibm-watson/speech-to-text/v1');
  5.  
  6. const speechToText = new SpeechToTextV1({
  7.     authenticator: new IamAuthenticator({ apikey: process.env.WATSON_SPEECH_TO_TEXT_API_KEY }),
  8.     serviceUrl: process.env.WATSON_SPEECH_TO_TEXT_URL,
  9.     language: 'hu-HU',
  10. });
  11.  
  12. const client = new Client({
  13.     intents: [
  14.         GatewayIntentBits.Guilds,
  15.         GatewayIntentBits.GuildMembers,
  16.         GatewayIntentBits.GuildVoiceStates,
  17.     ],
  18. });
  19.  
  20. let voiceConnection = null;
  21. let transcribing = false;
  22.  
  23. client.on("ready", () => {
  24.     console.log(`Logged in as ${client.user.tag}`);
  25.     // Register slash commands
  26.     client.guilds.cache.forEach((guild) => {
  27.         guild.commands.create({
  28.             name: "pv",
  29.             description: "Perform actions related to voice channels",
  30.             options: [
  31.                 {
  32.                     name: "action",
  33.                     description: "Choose an action",
  34.                     type: ApplicationCommandOptionType.String,
  35.                     required: true,
  36.                     choices: [
  37.                         {
  38.                             name: "Join",
  39.                             value: "join",
  40.                         },
  41.                         {
  42.                             name: "Leave",
  43.                             value: "leave",
  44.                         },
  45.                         {
  46.                             name: "Transcribe",
  47.                             value: "transcribe",
  48.                         },
  49.                         {
  50.                             name: "Ignore",
  51.                             value: "ignore",
  52.                         },
  53.                     ],
  54.                 },
  55.             ],
  56.         });
  57.     });
  58. });
  59.  
  60. client.on("interactionCreate", async (interaction) => {
  61.     if (!interaction.isCommand()) return;
  62.  
  63.     const { commandName, options } = interaction;
  64.     const action = options.getString("action");
  65.  
  66.     if (commandName === "pv") {
  67.         if (action === "join") {
  68.             if (!interaction.member.voice.channel) {
  69.                 interaction.reply("You are not in a voice channel.");
  70.                 return;
  71.             }
  72.  
  73.             if (voiceConnection) {
  74.                 interaction.reply("I am already in a voice channel.");
  75.                 return;
  76.             }
  77.  
  78.             voiceConnection = joinVoiceChannel({
  79.                 channelId: interaction.member.voice.channelId,
  80.                 guildId: interaction.guildId,
  81.                 adapterCreator: interaction.guild.voiceAdapterCreator,
  82.                 selfDeaf: false
  83.             });
  84.  
  85.             // const connectionReady = new Promise(resolve => {
  86.             //     const onStatusChange = (oldStatus, newStatus) => {
  87.             //         // console.log(oldStatus, newStatus);
  88.  
  89.  
  90.             //         if (newStatus === 'ready') {
  91.             //             resolve();
  92.             //             voiceConnection.off('stateChange', onStatusChange);
  93.             //         }
  94.             //     };
  95.                
  96.             //     voiceConnection.on('stateChange', onStatusChange);
  97.             // });
  98.              
  99.             // await connectionReady;
  100.              
  101.  
  102.             console.log(
  103.                 `Joined voice channel ${interaction.member.voice.channel.name}`
  104.             );
  105.             interaction.reply(
  106.                 `Joined voice channel ${interaction.member.voice.channel.name}.`
  107.             );
  108.         }
  109.  
  110.         if (action === "leave") {
  111.             if (!voiceConnection) {
  112.                 interaction.reply("I am not in a voice channel.");
  113.                 return;
  114.             }
  115.  
  116.             const channel = await client.channels.fetch(
  117.                 voiceConnection.joinConfig.channelId
  118.             );
  119.             voiceConnection.destroy();
  120.             voiceConnection = null;
  121.             transcribing = false;
  122.  
  123.             console.log(`Left voice channel ${channel.name}`);
  124.             interaction.reply(`Left voice channel ${channel.name}.`);
  125.         }
  126.        
  127.         if (action === "transcribe") {
  128.             if (!voiceConnection) {
  129.                 interaction.reply("I am not in a voice channel.");
  130.                 return;
  131.             }
  132.  
  133.             // console.log("======================= start =======================");
  134.             // console.log("_______ voiceConnection _______");
  135.             // console.log("||||| events |||||");
  136.             // console.log(voiceConnection._events);
  137.             // console.log("----------------------------");
  138.             // console.log("_______ Receiver _______");
  139.             // console.log("||||| main |||||");
  140.             // console.log(voiceConnection.receiver);
  141.             // console.log("-----------");
  142.             // console.log("||||| events |||||");
  143.             // console.log(voiceConnection.receiver._events);
  144.             // console.log("----------------------------");
  145.             // console.log("_______ Speaking _______");
  146.             // console.log("||||| main |||||");
  147.             // console.log(voiceConnection.receiver.speaking);
  148.             // console.log("-----------");
  149.             // console.log("||||| events |||||");
  150.             // console.log(voiceConnection.receiver.speaking._events);
  151.             // console.log("-----------");
  152.             // console.log("||||| users |||||");
  153.             // console.log(voiceConnection.receiver.speaking.users);
  154.             // console.log("-----------");
  155.             // console.log("||||| speaking timeouts |||||");
  156.             // console.log(voiceConnection.receiver.speaking.speakingTimeouts);
  157.             // console.log("======================= end =======================");
  158.  
  159.             if (transcribing) {
  160.                 interaction.reply("I am already transcribing.");
  161.                 return;
  162.             }
  163.  
  164.             transcribing = true;
  165.             const channel = await client.channels.fetch(
  166.                 voiceConnection.joinConfig.channelId
  167.             );
  168.             // console.log("--------start--------");
  169.  
  170.             // console.log("before voiceConnection.on('socketSpeaking', (user, speaking) => {...})");
  171.            
  172.             client.on('guildMemberSpeaking', (user, speaking) => {
  173.                 console.log(user.name, "is talking?", speaking);
  174.                 console.log("1");
  175.             })
  176.             voiceConnection.on('Speaking', (user, speaking) => {
  177.                 console.log("2");
  178.             })
  179.  
  180.             voiceConnection.on('ready', () => {
  181.                 console.log("voice connection ready");
  182.                 voiceConnection.on('socketSpeaking', (user, speaking) => {
  183.                     console.log("in voiceConnection.on('socketSpeaking', (user, speaking) => {...})");
  184.                    
  185.                    
  186.                     if (speaking) {
  187.                         console.log("someone is speaking");
  188.                         const audioResource = createAudioResource(user.audioStream, {
  189.                             inputType: "opus",
  190.                         });
  191.  
  192.                         audioResource.on("data", (data) => {
  193.                             console.log("setting audio resource");
  194.  
  195.                             // send data to IBM Watson Speech to Text
  196.                             const recognizeParams = {
  197.                                 audio: data,
  198.                                 contentType: 'audio/ogg;codecs=opus',
  199.                                 model: 'hu-HU_BroadbandModel'
  200.                             };
  201.  
  202.                             speechToText
  203.                                 .recognize(recognizeParams)
  204.                                 .then((transcript) => {
  205.                                     console.log(`Transcript: ${transcript.result.text}`);
  206.                                 })
  207.                                 .catch((err) => {
  208.                                     console.error("error:", err);
  209.                                 });
  210.                         });
  211.                     }
  212.                 });
  213.             });
  214.  
  215.             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.
  216.             interaction.reply(
  217.                 `Started transcribing in voice channel ${channel.name}.`
  218.             );
  219.         }
  220.  
  221.         if (action === "ignore") {
  222.             if (!transcribing) {
  223.                 interaction.reply("I am not currently transcribing.");
  224.                 return;
  225.             }
  226.  
  227.             if (!voiceConnection) {
  228.                 interaction.reply("I am not in a voice channel.");
  229.                 return;
  230.             }
  231.  
  232.             transcribing = false;
  233.             const channel = await client.channels.fetch(
  234.                 voiceConnection.joinConfig.channelId
  235.             );
  236.             console.log(`Stopped transcribing in voice channel ${channel.name}`);
  237.             interaction.reply(
  238.                 `Stopped transcribing in voice channel ${channel.name}.`
  239.             );
  240.         }
  241.     }
  242. });
  243.  
  244. client.login(process.env.DISCORD_TOKEN);
  245.  
Advertisement
Add Comment
Please, Sign In to add comment