discordjs / discord.js

A powerful JavaScript library for interacting with the Discord API
https://discord.js.org
Apache License 2.0
25.38k stars 3.97k forks source link

"speaking" event not triggering #3591

Closed tony-ist closed 4 years ago

tony-ist commented 4 years ago

There are 2 cases. In first one you speak during bot playing audio and then 'speaking' event will not trigger. In the second one you mute your mic, wait until "I am ready to listen..." and then unmute and speak, then the event triggers.


require('dotenv').config()
const Discord = require('discord.js')
const config = require('./config')

async function playFile(connection, filePath) {
  return new Promise((resolve, reject) => {
    const dispatcher = connection.play(filePath)
    dispatcher.setVolume(1)
    dispatcher.on('start', () => {
      console.log('Playing')
    })
    dispatcher.on('end', () => {
      resolve()
    })
    dispatcher.on('error', (error) => {
      console.error(error)
      reject(error)
    })
  })
}

const discordClient = new Discord.Client()

discordClient.on('ready', () => {
  console.log(`Logged in as ${discordClient.user.tag}!`)
})

discordClient.on('presenceUpdate', async (oldPresence, newPresence) => {
  const member = newPresence.member
  const presence = newPresence
  const memberVoiceChannel = member.voice.channel

  if (!presence || !presence.activity || !presence.activity.name || !memberVoiceChannel) {
    return
  }

  const connection = await memberVoiceChannel.join()

  await playFile(connection, 'anyaudio.mp3')

  console.log('I am ready to listen...')

  connection.on('speaking', (user, speaking) => {
    if (!speaking) {
      console.log('not speaking')
      return
    }

    console.log(`I'm listening to ${user.username}`)
  })
})

discordClient.login(config.discordApiToken)

Further details:

UPDATE 24.11.19: As far as I can tell in VoiceWebSocket#onMessage only one event signaling that user started speaking comes. Then there is nothing and audio stream never ends.

karyeet commented 4 years ago

Experiencing the same or similar issue.

Speaking event will never trigger when I speak in a voicechannel that my bot had also joined.

A suggested fix for a like issue in January failed to work for me as well. https://github.com/discordjs/discord.js/issues/2929#issuecomment-458584532

Using master version of discord.js node v8.15.1

Edit: updated node version to v10.15.3 Issue persists.

monbrey commented 4 years ago

Using master version of discord.js node v8.15.1

Given that djs master has a minimum Node requirement of v10, I'm wondering how you don't have far more issues than just the speaking event.

tony-ist commented 4 years ago

It's funny that with node js v8 + djs v11.5 the same issue is having place. That is why I think something changed in Discord API itself and it's not a djs issue really.

I mean I had audio stream END happily triggering before and then it just broke.

SpaceEEC commented 4 years ago

See #3524 and #3578 for more info.

SpaceEEC commented 4 years ago

Closing as this issue has been fixed with #3578.

castdrian commented 4 years ago

This has not been fixed with #3578 and still requires to manually sent a silence buffer.

SpaceEEC commented 4 years ago

I can't seem to reproduce this. For reference, my code and dependencies here.

Note that discord.js has been sending this silence buffer on its own before marking the connection as ready since https://github.com/discordjs/discord.js/commit/f826c9c75e976a8dca3fb4c88c130a1e12147a80 (February 2019).


If you still can reproduce this somehow, open a new issue filling out the template.