Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const Discord = require('discord.js');
- var tmi = require("tmi.js");
- var opus = require('node-opus');
- const fs = require('fs');
- var mkfifo = require('mkfifo').mkfifo;
- var ffmpeg = require('fluent-ffmpeg');
- const child_process = require('child_process');
- //twitch channel options
- var options = {
- options: {
- debug: true
- },
- connection: {
- reconnect: true
- },
- identity: {
- username: "twitchbotdebate",
- password: "oauth:fwerpbx4w9j3hm0n17sahikmispdgd"
- },
- channels: ["#twitchbotdebate"]
- };
- const discordClient = new Discord.Client();
- //var twitchClient = new tmi.client(options);
- var associativeArrayChatNamesVotes = new Array();
- var counter = 0;
- //Users Queued for debate
- var Queue = function() {
- this.first = null;
- this.last = null;
- this.size = 0;
- };
- var Node = function(data) {
- this.data = data;
- this.next = null;
- };
- Queue.prototype.enqueue = function(data) {
- var node = new Node(data);
- if (!this.first){
- this.first = node;
- this.last = this.first ;
- } else {
- this.last.next = node;
- this.last = this.last.next;
- }
- this.size += 1;
- return node;
- };
- Queue.prototype.dequeue = function() {
- temp = this.first;
- this.first = this.first.next;
- this.size -= 1;
- return temp.data;
- };
- var userQue = new Queue();
- mkfifo('/tmp/audiopipe', 0600, function(err) {
- console.log('error was :'+err);
- });
- var writestream = fs.createWriteStream('/tmp/audiopipe');
- let doCommand = (command) => {
- console.log("doing command");
- return new Promise((resolve, reject) => {
- console.log('new command');
- let child = child_process.spawn(command, { shell: true });
- let string = '';
- child.stderr.on('data', (data) => {
- string += data;
- });
- child.on('close', (code) => {
- console.log(code);
- if (code !== 0) {
- console.log(string);
- }
- resolve(code);
- });
- child.on('error', (err) => {
- console.log(err);
- reject(err);
- });
- });
- };
- //doCommand('ffmpeg -loop 1 -i image.png -f s16le -ar 48000 -ac 2 -i /tmp/audiopipe -c:v libx264 -tune stillimage -pix_fmt yuv420p -f flv rtmp://live-ord.twitch.tv/app/live_156820183_rfbIWfbKRldOrgALSw9IBSSAKqY06m');
- doCommand('ffmpeg -loop 1 -i image.png -f s16le -ar 48000 -ac 2 -i /tmp/audiopipe -c:v libx264 -tune stillimage -pix_fmt yuv420p -f flv rtmp://live-ord.twitch.tv/app/live_156820183_rfbIWfbKRldOrgALSw9IBSSAKqY06m');
- //ffmpeg -loop 1 -i image.jpg -i audio.wav -c:v libx264 -tune stillimage -c:a aac -b:a 192k -pix_fmt yuv420p -shortest out.mp4
- //ffmpeg -loop 1 -f image2 -i img.png -c:v libx264 -t 30 out.mp4
- discordClient.on('ready', () => {
- console.log('I am ready!');
- var channelID = getChannelId("Debate");
- var connection1;
- channelID.join().then(connection => {
- const receiver = connection.createReceiver();
- receiver.on('pcm', (user, buffer) =>{
- for(var i=0; i<buffer.length; i++){
- buffer[i] = (buffer[i]*32767);
- //console.log(buffer[i]);
- }
- //console.log(buffer);
- writestream.write(buffer)
- });
- });
- });
- /*
- discordClient.on('guildMemberSpeaking', (member, speaking) => {
- console.log('user: ' + member.user.username + " is " + speaking);
- if(!speaking){
- var zero = new Array(3280).fill(0);
- writestream.write(zero);
- }
- });
- */
- function queUser(user){
- userQue.enqueue(user);
- }
- function moveUserToVoiceChannel(user, channel){
- user.setVoiceChannel(channel);
- }
- function setUserRole(user, roleName){
- roleID = getRoleId(roleName);
- console.log("role id was : " + roleID);
- user.addRole(roleID).catch(error => console.log(error));
- }
- function getRoleId(roleName){
- //console.log("rolename: " + roleName)
- var returnID = null;
- discordClient.guilds.find(val => {
- val.roles.find(x => {
- if(x.name == roleName){
- console.log(x.id);
- console.log(x.name);
- returnID = x.id;
- }
- });
- });
- return returnID;
- }
- function getChannelId(channelName){
- //console.log("rolename: " + roleName)
- var returnID = null;
- discordClient.guilds.find(val => {
- val.channels.find(x => {
- if(x.name == channelName){
- console.log(x.id);
- console.log(x.name);
- returnID = x;
- }
- });
- });
- console.log(returnID.id);
- return returnID;
- }
- function isUserPartOfRole(username){
- //check if a user is part of the role group
- discordClient.guilds.find(val => {
- val.members.find(x => {
- console.log(x.user.username);
- //console.log(x.roles)
- x.roles.find(y => {
- if(y.name === 'queued_users'){
- console.log("is part of que rol");
- console.log(y.id);
- }
- })
- });
- });
- }
- function addUserVoiceChannel(user, channel){
- var channelID = getChannelId(channel);
- while(i<5){
- }
- //console.log(user);
- //before moving into the voice channel, PM the user to join a voice channel when they are the next in line
- //check if user is in the voice channel here
- //if they are not, then move on to the next user in the queue
- user.setVoiceChannel(channelID).catch(error => console.log(error));
- }
- function removeUserVoiceChannel(member){
- /*Since you cannot forcibly connect/disconnect a user from voice chat
- we must create a temporary channel, move the user to the temp channel
- then delete the temp channel
- */
- message.guild.createChannel('Temp', 'voice').then(function(){
- var channelID = getChannelId("Temp");
- message.member.setVoiceChannel(channelID).then(function(){
- channelID.delete();
- })
- })
- }
- function moveGuyAndMsg(channel){
- var channelID = getChannelId(channel);
- var curUser = userQue.dequeue();
- var role = getRoleId('queued_users');
- console.log(curUser);
- curUser.removeRole(role);
- curUser.setVoiceChannel(channelID).catch((error) => {console.log(error)}) //do move next que guy here});
- var temp = userQue.first;
- while(temp){
- console.log(temp.data.user.username);
- temp = temp.next;
- }
- };
- function sendDM(user){
- user.createDM().then( (DMChan) => {
- DMChan.send('You will soon be moved into voice chan, please make sure you are connected to a voice chan since we cannot focibly connect you to a voice chan ').catch()//user was probs not on or left guild, you should probs handle error);
- //DMChan.delete();
- }).catch()//user was probs not on or left guild, you should probs handle error);
- };
- function printQue(){
- var temp = userQue.first;
- while(temp){
- console.log(temp.data.user.username);
- temp = temp.next;
- }
- };
- function handleDiscordMsg(message){
- if(message.content == "DM"){
- sendDM(message.member);
- }
- if(message.channel.name == "join_debate_queue"){
- //Set user Role to queued user and add them to the Que
- setUserRole(message.member, "queued_users");
- queUser(message.member);
- printQue();
- }
- if(message.content == "timeup"){
- moveGuyAndMsg('Debate');
- ////Set user Role to queued user and add them to the Que
- //setUserRole(message.member, "queued_users");
- //queUser(message.member);
- //printQue();
- }
- if(message.content == "join"){
- //Set user Role to queued user and add them to the Que
- addUserVoiceChannel(message.member, "Debate");
- //queUser(message.member);
- }
- };
- //twitchClient.on("chat", (channel, user, message, self) => handleTwitchMsg(channel, user, message, self));
- discordClient.on('message', message => handleDiscordMsg(message));
- // Connect the client to the twitch chat server..
- discordClient.login('MzExMDQ2NDM0NTMzNDA4Nzc5.C_KGZA.2MxcXY_prKG2u7GvNUwL8gdWkuw');
- //twitchClient.connect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement