Search code examples
node.jsdiscorddiscord.jsbotseris

Call a user directly using Eris


I am trying to make a bot with Eris and I need it to be able to call people in direct messaging. This is what I came up with, but clearly, I'm missing something as it does not work.

                        if (contacts[name].dm) {
                            const channel = await bot.getDMChannel(contacts[name].id)
                            await channel.ring([contacts[name].id])
                        }else{
                            bot.joinVoiceChannel(contacts[name].id)
                        }

Solution

  • Might not be the most effective or clean way to do it, but I managed.

    function discordAPI(authToken,apiEndpoint,JSONparams,type="GET") {
        var xhr=new XMLHttpRequest()
        console.log("Connecting to https://discord.com/api/v9"+apiEndpoint)
        xhr.open(type,"https://discord.com/api/v9"+apiEndpoint,true)
        xhr.setRequestHeader('Content-Type','application/json')
        xhr.setRequestHeader('Authorization',authToken)
        console.log(authToken)
        xhr.onload = function () {
            console.log('DONE: ', xhr.status);
            console.log('REPONSE: ', xhr.reponseText);
            // console.log('=======================');
            // console.log('REPONSE_HEADERS:\n', xhr.getAllResponseHeaders());
            // console.log('=======================')
          };
        xhr.send(JSON.stringify(JSONparams))
    }
    
    //...
    
    const channel = await bot.getDMChannel(id)
    await sleep(time*1000)
    discordAPI(bot.token, ('/channels/' + channel.id + '/call/ring'), '', 'POST')
    bot.joinVoiceChannel(channel.id).then((vconnect) => { //THIS IS NECESSARY TO JOIN THE CALL!!
        console.log(vconnect)
        if(sf!='null'){vconnect.play(__dirname + "/../sfx/" + sf + ".mp3")}
        vconnect.once("userDisconnect", () => { if(userID == id){bot.switchChannel(null)} });
        vconnect.once("end", () => { bot.switchChannel(null) });
        vconnect.once("error", () => { console.log("----\n"+Error.toString()+"----\n") });
    })
    await sleep(timespent*60*1000)
    bot.disconnect()
    console.log('Called.')
    bot.connect()