Rei-x / discord-speech-recognition

Speech to text extension for discord.js
https://npmjs.com/package/discord-speech-recognition
MIT License
56 stars 22 forks source link

Callback firing multiple times for the same message when using Wit.ai #17

Closed lmmfranco closed 2 years ago

lmmfranco commented 2 years ago

DiscordSR seems to be sending multiple requests when using wit.ai.

Code used for this test:

const { Client, Intents } = require('discord.js');
const { addSpeechEvent, resolveSpeechWithWitai } = require("discord-speech-recognition");
const { joinVoiceChannel } = require("@discordjs/voice");

const client = new Client({
    intents: [
        Intents.FLAGS.GUILDS,
        Intents.FLAGS.GUILD_VOICE_STATES,
        Intents.FLAGS.GUILD_MESSAGES,
    ],
});

addSpeechEvent(client, {
    speechRecognition: resolveSpeechWithWitai,
    lang: 'pt-BR',
    key: '<my-witai-key>',
    profanityFilter: false,
});

client.on('messageCreate', (msg) => {
    console.log("[M]", msg.member.displayName, msg.content);
    if (msg.content.startsWith('##join') && msg.member?.voice.channel) {
        joinVoiceChannel({
            channelId: msg.member.voice.channelId,
            guildId: msg.member.guild.id,
            adapterCreator: msg.member.guild.voiceAdapterCreator,
            selfDeaf: false,
            selfMute: true,
        });
    }
});

client.on('speech', (msg) => {
    console.log(`[S] ${msg.member.displayName}: ${msg.content}`);
});

client.on("ready", () => {
    console.log("BOT READY");
})

client.login('<my-bot-key>');

Resulting log:

BOT READY
[M] darkbreaker ##join
[S] darkbreaker: oi teste um dois três
[S] darkbreaker: oi teste um, dois, três

Both even show up on wit.ai dashboard:
image

When using just addSpeechEvent(client); with the default google SR it worked as intended:

BOT READY
[M] darkbreaker ##join
[S] darkbreaker: this is a test
Rei-x commented 2 years ago

I'm sorry, but I can't reproduce it. When I run your code, I get correct output: image Do you use the latest version of the package?

And also, you don't have to specify language for wit.ai in speechOptions (cuz it doesn't have any effect), you do it in your app settings: image

lmmfranco commented 2 years ago

It seems to be related to how many people are connected to the channel. I had another (silent) bot join. Every time it would join the SR bot add another copy of every sound spoken:

BOT READY
[M] darkbreaker ##join
[S] darkbreaker: This is that one.
[M] darkbreaker ..join
[S] darkbreaker: This is test two.
[S] darkbreaker: This is test too.
[M] darkbreaker ..leave
[M] darkbreaker ..join
[S] darkbreaker: This is test three.
[S] darkbreaker: This is test three.
[S] darkbreaker: This is test three.

I will try and debug the code when I have more time in order to provide better feedback

lmmfranco commented 2 years ago

I guess this is the relevant bit: https://github.com/Rei-x/discord-speech-recognition/blob/27d28486c9f5253189636d45fce51c9323b49810/src/bot/events/speech.ts#L61-L66

There is a receiver being created every time anyone joins the channel. There's only need for one receiver to be in place, as it will fire serveral times as long as it is subscribed to.

There could be a check to run the code when the bot itself joins.

In @discordjs/voice example they setup the receiver on the /join command of their bot: https://github.com/discordjs/voice/blob/main/examples/recorder/src/interactions.ts

Rei-x commented 2 years ago

Yes, it's exactly what causes the issue, I'll fix it as soon as possible. Thank you a lot for help!

Rei-x commented 2 years ago

Fixed in #18 Install new version of package (2.1.0) and check if it now works.