Advertisement
plytalent

bot.js

Jan 11th, 2021
820
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.94 KB | None | 0 0
  1. const redis = require('redis')
  2. const redisClient = redis.createClient()
  3. const prism = require('prism-media');
  4. const { inspect } = require('util');
  5. const Discord = require("discord.js");
  6. const { prefix, token } = require("./config.json");
  7. const ytdl = require("ytdl-core");
  8. const { Worker, isMainThread, parentPort } = require('worker_threads');
  9. const client = new Discord.Client();
  10. var volume = new Map();
  11. redisClient.get("Volume",(err,data)=>{
  12.   if (!err && data){
  13.     volume = new Map(Object.entries(data));
  14.   } else {
  15.     volume = new Map();
  16.   }
  17. })
  18. const dispatcher = new Map();
  19. const queue = new Map();
  20. const eventschid = "797505130210656316"
  21. var waitingownerid = true
  22. var ownerid
  23. redisClient.get('ownerid',(error,data)=>{
  24.   if (error){
  25.     console.error(error)
  26.   } else {
  27.     ownerid = data
  28.     console.log(data,ownerid)
  29.   }
  30.   waitingownerid = false
  31. })
  32. function wait(ms) {
  33.     const end = Date.now() + ms;
  34.     while (Date.now() < end) { }
  35. }
  36. wait(2000)
  37.  
  38.  
  39. client.once("ready", () => {
  40.   console.log("Ready!");
  41. });
  42.  
  43. client.once("reconnecting", () => {
  44.   console.log("Reconnecting!");
  45. });
  46.  
  47. client.once("disconnect", () => {
  48.   console.log("Disconnect!");
  49. });
  50. const lucene_live = new Worker("./loopevent_check_live_lucene.js");
  51. lucene_live.on('message', (msg) => {
  52.   console.log(`lucene_live: ${msg}`);
  53.   if(msg=="LIVE Lucene"){
  54.     client.emit("Check_PolygonProject_Live",msg)
  55.   }
  56. });
  57. client.on("Check_PolygonProject_Live",(msg)=>{
  58.   if (client.channels.cache.get(eventschid)){
  59.     client.channels.cache.get(eventschid).send(`${msg}`)
  60.   } else {
  61.     console.warn("client.channels.cache.get(eventschid) == NULL")
  62.   }
  63. })
  64.  
  65. client.on("message", async message => {
  66.   if (message.author.bot) return;
  67.   if (!message.content.startsWith(prefix)) return;
  68.  
  69.   const serverQueue = queue.get(message.guild.id);
  70.   if (message.content.startsWith(`${prefix}play`)) {
  71.     execute(message, serverQueue);
  72.     return;
  73.   } else if (message.content.startsWith(`${prefix}skip`)) {
  74.     skip(message, serverQueue);
  75.     return;
  76.   } else if (message.content.startsWith(`${prefix}stop`)) {
  77.     stop(message, serverQueue);
  78.     return;
  79.   } else if (message.content.startsWith(`${prefix}loop`)) {
  80.     loop(message, serverQueue);
  81.     return;
  82.   } else if (message.content.startsWith(`${prefix}eval`)) {
  83.     if (message.author.id == ownerid){
  84.       execution_eval(message)
  85.     } else {
  86.       console.log(message.author.id == ownerid, message.author.id, ownerid)
  87.     }
  88.     return
  89.   } else if (message.content.startsWith(`${prefix}volume`)) {
  90.     volumefunc(message, serverQueue, volume.get(message.guild.id));
  91.     return;
  92.   }
  93. });
  94. async function execution_eval(message){
  95.   const args = message.content.split(' ');
  96.   const command = args.shift().toLowerCase();
  97.   let evaled;
  98.   try {
  99.     evaled = await eval(args.join(' '));
  100.     if (new String(evaled).length <= 1994){
  101.       message.channel.send("```"+inspect(evaled)+"```");
  102.     } else {
  103.       message.channel.send("message.length > 2000\nlength:"+new String(evaled).length);
  104.       console.log(new String(evaled).length)
  105.     }
  106.     console.log(inspect(evaled));
  107.   }
  108.   catch (error) {
  109.     console.error(error);
  110.     message.reply('there was an error during evaluation.');
  111.   }
  112. }
  113. async function execute(message, serverQueue) {
  114.   const args = message.content.split(" ");
  115.  
  116.   const voiceChannel = message.member.voice.channel;
  117.   if (!voiceChannel)
  118.     return message.channel.send(
  119.       "You need to be in a voice channel to play music!"
  120.     );
  121.   const permissions = voiceChannel.permissionsFor(message.client.user);
  122.   if (!permissions.has("CONNECT") || !permissions.has("SPEAK")) {
  123.     return message.channel.send(
  124.       "I need the permissions to join and speak in your voice channel!"
  125.     );
  126.   }
  127.  
  128.   const songInfo = await ytdl.getInfo(args[1]);
  129.   const song = {
  130.         title: songInfo.videoDetails.title,
  131.         url: songInfo.videoDetails.video_url,
  132.    };
  133.   if (!volume.get(message.guild.id)){
  134.    volume.set(message.guild.id,100)
  135.   }
  136.   if (!dispatcher.get(message.guild.id)){
  137.     dispatcher.set(message.guild.id,null)
  138.   }
  139.   if (!serverQueue) {
  140.     const queueContruct = {
  141.       textChannel: message.channel,
  142.       voiceChannel: voiceChannel,
  143.       //connection: null,
  144.       songs: [],
  145.       playing: true,
  146.       looped: false,
  147.       skipping:false
  148.     };
  149.  
  150.     queue.set(message.guild.id, queueContruct);
  151.  
  152.     queueContruct.songs.push(song);
  153.  
  154.     try {
  155.       var connection = await voiceChannel.join();
  156.       dispatcher.set(message.guild.id,connection);
  157.       play(message.guild, queueContruct.songs[0]);
  158.     } catch (err) {
  159.       console.log(err);
  160.       queue.delete(message.guild.id);
  161.       return message.channel.send(err);
  162.     }
  163.   } else {
  164.     serverQueue.songs.push(song);
  165.     return message.channel.send(`${song.title} has been added to the queue!`);
  166.   }
  167. }
  168. function volumefunc(message, serverQueue, Volume) {
  169.   if (!message.member.voice.channel)
  170.     return message.channel.send(
  171.       "You have to be in a voice channel to Change Volume!"
  172.     );
  173.   if(!serverQueue)
  174.     return message.channel.send("There is no song that I could adjust volume!");
  175.   const args = message.content.split(" ");
  176.   Volume = args[1]
  177.   dispatcher.get(message.guild.id).dispatcher.setVolume(Volume / 100);
  178. //  console.log(serverQueue.connection.dispatcher.constructor)
  179.   redisClient.set("Volume", JSON.stringify(Object.fromEntries(volume)))
  180.   message.channel.send(`volume:${Volume}`);
  181.   //redisClient.set("Volume", JSON.stringify(Object.fromEntries(volume)))
  182. }
  183. function skip(message, serverQueue) {
  184.   if (!message.member.voice.channel)
  185.     return message.channel.send(
  186.       "You have to be in a voice channel to stop the music!"
  187.     );
  188.   if (!serverQueue)
  189.     return message.channel.send("There is no song that I could skip!");
  190.   if (serverQueue.looped){
  191.     serverQueue.looped = false;
  192.     wait(100)
  193.     dispatcher.get(message.guild.id).dispatcher.end();
  194.     serverQueue.looped = true;
  195.   } else {
  196.     dispatcher.get(message.guild.id).dispatcher.end();
  197.   }
  198. }
  199. function loop(message, serverQueue) {
  200.   if (!message.member.voice.channel)
  201.     return message.channel.send(
  202.       "You have to be in a voice channel to stop the music!"
  203.     );
  204.   if(!serverQueue)
  205.     return message.channel.send("There is no song that I could Loop!");
  206.   serverQueue.looped = !serverQueue.looped
  207.   message.channel.send(`Loop:${serverQueue.looped}`);
  208. }
  209. function stop(message, serverQueue) {
  210.   if (!message.member.voice.channel)
  211.     return message.channel.send(
  212.       "You have to be in a voice channel to stop the music!"
  213.     );
  214.  
  215.   if (!serverQueue)
  216.     return message.channel.send("There is no song that I could stop!");
  217.  
  218.   serverQueue.songs = [];
  219.   dispatcher.get(message.guild.id).dispatcher.end();
  220. }
  221.  
  222. function play(guild, song) {
  223.   const serverQueue = queue.get(guild.id);
  224.   //console.log(`Guild:${guild.id}\nloop:${serverQueue.looped}\ncurrent song:${song.title}\nnext song:${serverQueue.songs[1]}`)
  225.   if (!song) {
  226.     console.log(`Guild:${guild.name}\nloop:${serverQueue.looped}\ncurrent song:NULL\nnext song:NULL`)
  227.     serverQueue.voiceChannel.leave();
  228.     //queue.delete(guild.id);
  229.     return;
  230.   }
  231.   console.log(`Guild:${guild.name}\nloop:${serverQueue.looped}\ncurrent song:${song.title}\nnext song:${serverQueue.songs[1]}`)
  232.  
  233.     dispatcher.get(guild.id).play(ytdl(song.url, { quality: 'highestaudio' }))
  234.     dispatcher.get(guild.id).dispatcher.on("finish", () => {
  235.       if (!serverQueue.looped){
  236.         serverQueue.songs.shift();
  237.       }
  238.       play(guild, serverQueue.songs[0]);
  239.    })
  240.    dispatcher.get(guild.id).dispatcher.on("error", error => console.error(error));
  241.    dispatcher.get(guild.id).dispatcher.setVolume(volume.get(guild.id) / 100);
  242.   //serverQueue.textChannel.send(`Start playing: **${song.title}**`);
  243. }
  244.  
  245. console.log(`prefix:${prefix}\ntoken:${token}`)
  246. client.login(token);
  247. redisClient.set("Volume", JSON.stringify(Object.fromEntries(volume)))
  248. console.log(JSON.stringify(Object.fromEntries(volume)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement