Skip to content
This repository has been archived by the owner on Feb 13, 2022. It is now read-only.

Use 2 Bots for use in ANY voice channel #4

Open
erdemozor opened this issue Jan 8, 2021 · 0 comments
Open

Use 2 Bots for use in ANY voice channel #4

erdemozor opened this issue Jan 8, 2021 · 0 comments

Comments

@erdemozor
Copy link

I found that it is possible to run two instances of the bot with different tokens one for joining and leaving.
This enables the bot to join any channel and not just a set voice channel like "General".

Sample code for anyone that wants to use this: (These two can be in the same dir as index.js and should be run at the same time.)

// Join.js
require('dotenv').config()
const Discord = require('discord.js');
const client = new Discord.Client();

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

client.on('voiceStateUpdate', async (oldState, newState) => {
  if (oldState.channel !== newState.channel) {

	if (newState.channel && !oldState.member.user.bot ) {
      //const havingAGoodTime = Math.random() <= 0.05;
      //const file = havingAGoodTime ? './sounds/connected-but-its-loud-af.mp3' : './sounds/connected.wav';
      //play(newState.channel, file);
	  play(newState.channel, './sounds/connected.wav');
	  console.log(`Joined ${newState.channel.name} for ${newState.member.user.tag} joining.`);
    }
  }
   if (oldState.selfDeaf && !newState.selfDeaf) {
    play(oldState.channel, './sounds/talkpower_granted.wav');
	console.log(`Joined ${oldState.channel.name} for ${oldState.member.user.tag} Undeafen.`);
  }
})

client.login(process.env.DISCORD_BOT_TOKEN);

async function play(channel, file)
{
  const connection = await channel.join();

  connection.play(file);
}

and

//Leave.js
require('dotenv').config()
const Discord = require('discord.js');
const client = new Discord.Client();

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

client.on('voiceStateUpdate', async (oldState, newState) => {
  if (oldState.channel !== newState.channel) {
	if (oldState.channel && !oldState.member.user.bot ) {
      play(oldState.channel, './sounds/disconnected.wav');
	  console.log(`Joined ${oldState.channel.name} for ${oldState.member.user.tag} leaving.`);
    }
	
  }
})

client.login("SECOND BOT TOKEN");

async function play(channel, file)
{
  const connection = await channel.join();

  connection.play(file);
}

For example; when you leave General1 to join General2, The first bot will go to General2 and the second bot will go to General1 at the same time.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant