Advertisement
Guest User

Untitled

a guest
May 19th, 2017
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.48 KB | None | 0 0
  1. const Discord = require('discord.js');
  2. var tmi = require("tmi.js");
  3. var opus = require('node-opus');
  4. const fs = require('fs');
  5. var mkfifo = require('mkfifo').mkfifo;
  6. var ffmpeg = require('fluent-ffmpeg');
  7. const child_process = require('child_process');
  8.  
  9. //twitch channel options
  10. var options = {
  11. options: {
  12. debug: true
  13. },
  14. connection: {
  15. reconnect: true
  16. },
  17. identity: {
  18. username: "twitchbotdebate",
  19. password: "oauth:fwerpbx4w9j3hm0n17sahikmispdgd"
  20. },
  21. channels: ["#twitchbotdebate"]
  22. };
  23.  
  24. const discordClient = new Discord.Client();
  25. //var twitchClient = new tmi.client(options);
  26.  
  27. var associativeArrayChatNamesVotes = new Array();
  28. var counter = 0;
  29.  
  30.  
  31. //Users Queued for debate
  32. var Queue = function() {
  33. this.first = null;
  34. this.last = null;
  35.  
  36. this.size = 0;
  37. };
  38.  
  39. var Node = function(data) {
  40. this.data = data;
  41. this.next = null;
  42. };
  43.  
  44. Queue.prototype.enqueue = function(data) {
  45. var node = new Node(data);
  46.  
  47. if (!this.first){
  48. this.first = node;
  49. this.last = this.first ;
  50. } else {
  51. this.last.next = node;
  52. this.last = this.last.next;
  53. }
  54.  
  55. this.size += 1;
  56. return node;
  57. };
  58.  
  59. Queue.prototype.dequeue = function() {
  60. temp = this.first;
  61. this.first = this.first.next;
  62. this.size -= 1;
  63. return temp.data;
  64. };
  65.  
  66.  
  67. var userQue = new Queue();
  68.  
  69.  
  70. mkfifo('/tmp/audiopipe', 0600, function(err) {
  71. console.log('error was :'+err);
  72. });
  73.  
  74.  
  75. var writestream = fs.createWriteStream('/tmp/audiopipe');
  76.  
  77.  
  78. let doCommand = (command) => {
  79. console.log("doing command");
  80. return new Promise((resolve, reject) => {
  81. console.log('new command');
  82. let child = child_process.spawn(command, { shell: true });
  83. let string = '';
  84. child.stderr.on('data', (data) => {
  85. string += data;
  86. });
  87. child.on('close', (code) => {
  88. console.log(code);
  89. if (code !== 0) {
  90. console.log(string);
  91. }
  92. resolve(code);
  93. });
  94. child.on('error', (err) => {
  95. console.log(err);
  96. reject(err);
  97. });
  98. });
  99. };
  100.  
  101.  
  102. //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');
  103. 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');
  104.  
  105.  
  106. //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
  107. //ffmpeg -loop 1 -f image2 -i img.png -c:v libx264 -t 30 out.mp4
  108.  
  109. discordClient.on('ready', () => {
  110. console.log('I am ready!');
  111.  
  112. var channelID = getChannelId("Debate");
  113. var connection1;
  114.  
  115. channelID.join().then(connection => {
  116. const receiver = connection.createReceiver();
  117.  
  118.  
  119. receiver.on('pcm', (user, buffer) =>{
  120. for(var i=0; i<buffer.length; i++){
  121. buffer[i] = (buffer[i]*32767);
  122. //console.log(buffer[i]);
  123. }
  124. //console.log(buffer);
  125. writestream.write(buffer)
  126. });
  127. });
  128. });
  129.  
  130.  
  131.  
  132. /*
  133. discordClient.on('guildMemberSpeaking', (member, speaking) => {
  134. console.log('user: ' + member.user.username + " is " + speaking);
  135.  
  136.  
  137. if(!speaking){
  138. var zero = new Array(3280).fill(0);
  139. writestream.write(zero);
  140. }
  141.  
  142. });
  143. */
  144.  
  145.  
  146. function queUser(user){
  147. userQue.enqueue(user);
  148. }
  149.  
  150. function moveUserToVoiceChannel(user, channel){
  151. user.setVoiceChannel(channel);
  152. }
  153.  
  154.  
  155. function setUserRole(user, roleName){
  156. roleID = getRoleId(roleName);
  157. console.log("role id was : " + roleID);
  158. user.addRole(roleID).catch(error => console.log(error));
  159. }
  160.  
  161. function getRoleId(roleName){
  162. //console.log("rolename: " + roleName)
  163. var returnID = null;
  164. discordClient.guilds.find(val => {
  165. val.roles.find(x => {
  166.  
  167. if(x.name == roleName){
  168. console.log(x.id);
  169. console.log(x.name);
  170. returnID = x.id;
  171. }
  172. });
  173. });
  174.  
  175. return returnID;
  176. }
  177.  
  178. function getChannelId(channelName){
  179. //console.log("rolename: " + roleName)
  180. var returnID = null;
  181. discordClient.guilds.find(val => {
  182. val.channels.find(x => {
  183.  
  184. if(x.name == channelName){
  185. console.log(x.id);
  186. console.log(x.name);
  187. returnID = x;
  188. }
  189. });
  190. });
  191. console.log(returnID.id);
  192. return returnID;
  193. }
  194.  
  195. function isUserPartOfRole(username){
  196. //check if a user is part of the role group
  197. discordClient.guilds.find(val => {
  198. val.members.find(x => {
  199. console.log(x.user.username);
  200. //console.log(x.roles)
  201. x.roles.find(y => {
  202. if(y.name === 'queued_users'){
  203. console.log("is part of que rol");
  204. console.log(y.id);
  205. }
  206. })
  207. });
  208. });
  209. }
  210.  
  211. function addUserVoiceChannel(user, channel){
  212. var channelID = getChannelId(channel);
  213.  
  214. while(i<5){
  215.  
  216. }
  217. //console.log(user);
  218.  
  219. //before moving into the voice channel, PM the user to join a voice channel when they are the next in line
  220.  
  221. //check if user is in the voice channel here
  222. //if they are not, then move on to the next user in the queue
  223. user.setVoiceChannel(channelID).catch(error => console.log(error));
  224. }
  225.  
  226. function removeUserVoiceChannel(member){
  227. /*Since you cannot forcibly connect/disconnect a user from voice chat
  228. we must create a temporary channel, move the user to the temp channel
  229. then delete the temp channel
  230. */
  231. message.guild.createChannel('Temp', 'voice').then(function(){
  232. var channelID = getChannelId("Temp");
  233. message.member.setVoiceChannel(channelID).then(function(){
  234. channelID.delete();
  235. })
  236. })
  237. }
  238.  
  239. function moveGuyAndMsg(channel){
  240. var channelID = getChannelId(channel);
  241. var curUser = userQue.dequeue();
  242.  
  243. var role = getRoleId('queued_users');
  244. console.log(curUser);
  245. curUser.removeRole(role);
  246.  
  247. curUser.setVoiceChannel(channelID).catch((error) => {console.log(error)}) //do move next que guy here});
  248. var temp = userQue.first;
  249. while(temp){
  250. console.log(temp.data.user.username);
  251. temp = temp.next;
  252. }
  253. };
  254.  
  255.  
  256. function sendDM(user){
  257. user.createDM().then( (DMChan) => {
  258. 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);
  259. //DMChan.delete();
  260. }).catch()//user was probs not on or left guild, you should probs handle error);
  261.  
  262.  
  263. };
  264.  
  265. function printQue(){
  266. var temp = userQue.first;
  267. while(temp){
  268. console.log(temp.data.user.username);
  269. temp = temp.next;
  270. }
  271. };
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279. function handleDiscordMsg(message){
  280.  
  281. if(message.content == "DM"){
  282. sendDM(message.member);
  283. }
  284.  
  285. if(message.channel.name == "join_debate_queue"){
  286. //Set user Role to queued user and add them to the Que
  287. setUserRole(message.member, "queued_users");
  288. queUser(message.member);
  289. printQue();
  290. }
  291. if(message.content == "timeup"){
  292. moveGuyAndMsg('Debate');
  293. ////Set user Role to queued user and add them to the Que
  294. //setUserRole(message.member, "queued_users");
  295. //queUser(message.member);
  296. //printQue();
  297. }
  298. if(message.content == "join"){
  299. //Set user Role to queued user and add them to the Que
  300. addUserVoiceChannel(message.member, "Debate");
  301. //queUser(message.member);
  302. }
  303.  
  304. };
  305.  
  306.  
  307. //twitchClient.on("chat", (channel, user, message, self) => handleTwitchMsg(channel, user, message, self));
  308. discordClient.on('message', message => handleDiscordMsg(message));
  309.  
  310.  
  311.  
  312. // Connect the client to the twitch chat server..
  313. discordClient.login('MzExMDQ2NDM0NTMzNDA4Nzc5.C_KGZA.2MxcXY_prKG2u7GvNUwL8gdWkuw');
  314. //twitchClient.connect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement