viki.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // Load up the discord.js library
  2. const Discord = require("discord.js");
  3. // Load up the shell command library
  4. var exec = require('child_process').exec;
  5. // Define a function to execute a command
  6. function execute(command, callback)
  7. {
  8. exec(command, function(error, stdout, stderr)
  9. {
  10. callback(stdout);
  11. });
  12. };
  13. // This is your client. Some people call it `bot`, some people call it `self`,
  14. // some might call it `cootchie`. Either way, when you see `client.something`, or `bot.something`,
  15. // this is what we're refering to. Your client.
  16. const client = new Discord.Client();
  17. // this allows us to define a voice connection with global scope
  18. var connection;
  19. var dispatcher;
  20. // Here we load the config.json file that contains our token and our prefix values.
  21. const config = require("./config.json");
  22. // config.token contains the bot's token
  23. // config.prefix contains the message prefix.
  24. client.on("ready", () =>
  25. {
  26. // This event will run if the bot starts, and logs in, successfully.
  27. console.log(`Bot has started, with ${client.users.size} users, in ${client.channels.size} channels of ${client.guilds.size} guilds.`);
  28. // Example of changing the bot's playing game to something useful. `client.user` is what the
  29. // docs refer to as the "ClientUser".
  30. client.user.setActivity("Taking Over Chicago");
  31. });
  32. client.on("guildCreate", guild =>
  33. {
  34. // This event triggers when the bot joins a guild.
  35. console.log(`New guild joined: ${guild.name} (id: ${guild.id}). This guild has ${guild.memberCount} members!`);
  36. client.user.setActivity(`Serving ${client.guilds.size} servers`);
  37. });
  38. client.on("guildDelete", guild => {
  39. // this event triggers when the bot is removed from a guild.
  40. console.log(`I have been removed from: ${guild.name} (id: ${guild.id})`);
  41. client.user.setActivity("Taking Over Chicago");
  42. });
  43. client.on("message", async message =>
  44. {
  45. // This event will run on every single message received, from any channel or DM.
  46. // It's good practice to ignore other bots. This also makes your bot ignore itself
  47. // and not get into a spam loop (we call that "botception").
  48. if (message.author.bot) return;
  49. // Also good practice to ignore any message that does not start with our prefix,
  50. // which is set in the configuration file.
  51. if (message.content.indexOf(config.prefix) !== 0) return;
  52. // Here we separate our "command" name, and our "arguments" for the command.
  53. // e.g. if we have the message "+say Is this the real life?" , we'll get the following:
  54. // command = say
  55. // args = ["Is", "this", "the", "real", "life?"]
  56. const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
  57. const command = args.shift().toLowerCase();
  58. // Let's go with a few common example commands! Feel free to delete or change those.
  59. if (command === "ping") {
  60. // Calculates ping between sending a message and editing it, giving a nice round-trip latency.
  61. // The second ping is an average latency between the bot and the websocket server (one-way, not round-trip)
  62. const m = await message.channel.send("Ping?");
  63. m.edit(`Pong! Latency is ${m.createdTimestamp - message.createdTimestamp}ms. API Latency is ${Math.round(client.ping)}ms`);
  64. }
  65. if (command === "say") {
  66. // makes the bot say something and delete the message. As an example, it's open to anyone to use.
  67. // To get the "message" itself we join the `args` back into a string with spaces:
  68. const sayMessage = args.join(" ");
  69. // Then we delete the command message (sneaky, right?). The catch just ignores the error with a cute smiley thing.
  70. message.delete().catch(O_o=>{});
  71. // And we get the bot to say the thing:
  72. message.channel.send(sayMessage);
  73. }
  74. if (command === "kick") {
  75. // This command must be limited to mods and admins. In this example we just hardcode the role names.
  76. // Please read on Array.some() returns true if any element meets the passed condition (map-like)
  77. if (!message.member.roles.some(r=>["Administrator", "Moderator","Admin","Mod"].includes(r.name)) )
  78. return message.reply("Sorry, you don't have permissions to use this!");
  79. // Let's first check if we have a member and if we can kick them!
  80. // message.mentions.members is a collection of people that have been mentioned, as GuildMembers.
  81. // We can also support getting the member by ID, which would be args[0]
  82. let member = message.mentions.members.first() || message.guild.members.get(args[0]);
  83. if (!member)
  84. return message.reply("Please mention a valid member of this server");
  85. if (!member.kickable)
  86. return message.reply("I cannot kick this user! Do they have a higher role? Do I have kick permissions?");
  87. // slice(1) removes the first part, which here should be the user mention or ID
  88. // join(' ') takes all the various parts to make it a single string.
  89. let reason = args.slice(1).join(' ');
  90. if (!reason) reason = "No reason provided";
  91. // Now, time for a swift kick in the nuts!
  92. await member.kick(reason)
  93. .catch(error => message.reply(`Sorry ${message.author} I couldn't kick because of : ${error}`));
  94. message.reply(`${member.user.tag} has been kicked by ${message.author.tag} because: ${reason}`);
  95. }
  96. if (command === "ban") {
  97. // Most of this command is identical to kick, except that here we'll only let admins do it.
  98. // In the real world mods could ban too, but this is just an example, right? ;)
  99. if (!message.member.roles.some(r=>["Administrator"].includes(r.name)) )
  100. return message.reply("Sorry, you don't have permissions to use this!");
  101. let member = message.mentions.members.first();
  102. if (!member)
  103. return message.reply("Please mention a valid member of this server");
  104. if (!member.bannable)
  105. return message.reply("I cannot ban this user! Do they have a higher role? Do I have ban permissions?");
  106. let reason = args.slice(1).join(' ');
  107. if (!reason) reason = "No reason provided";
  108. await member.ban(reason)
  109. .catch(error => message.reply(`Sorry ${message.author} I couldn't ban because of : ${error}`));
  110. message.reply(`${member.user.tag} has been banned by ${message.author.tag} because: ${reason}`);
  111. }
  112. if (command === "join")
  113. {
  114. // This command moves the bot to the channel given as the first arg.
  115. // This is the passed channel
  116. const channel = args.join(' ');
  117. const channelVar = message.guild.channels.find("name", channel);
  118. // Checks if channel is valid.
  119. if (!(message.guild.channels.exists("name", channel) && channelVar.type === "voice"))
  120. return message.reply(`Sorry ${message.author}, that channel doesn't appear to exist.`);
  121. // Joins the channel
  122. channelVar.join().then(conn =>
  123. {
  124. connection = conn;
  125. console.log('Connected!');
  126. }).catch(console.error);
  127. }
  128. if (command === "leave")
  129. {
  130. // this command remove the bot from the channel passed
  131. const channel = args.join(' ');
  132. const channelVar = message.guild.channels.find("name", channel);
  133. if (!(message.guild.channels.exists("name", channel) && channelVar.type === "voice"))
  134. return message.reply(`Sorry ${message.author}, that channel doesn't appear to exist.`);
  135. channelVar.leave();
  136. }
  137. if (command === "play")
  138. {
  139. // this command plays the song if one song matches the query given in beets
  140. const song = args.join(' '); // creates a single string of all args (the song)
  141. var path; // this will hold the filepath of our song
  142. exec(`beet ls -p title:${song} | wc -l`, function (error, stdout, stderr)
  143. {
  144. if (error)
  145. {
  146. return message.reply(`Sorry, I encountered an issue looking for that song: ${error}`);
  147. }
  148. else if (stdout !== '1\n')
  149. {
  150. return message.reply(`There were ${stdout} songs that matched your search. Please give the full song name, or wait until I become a less lazy dev.`);
  151. }
  152. else
  153. {
  154. exec(`beet ls -p title:${song}`, function (error, stdout, stderr)
  155. {
  156. if (error)
  157. {
  158. return message.reply(`Sorry, I encountered an issue looking for that song: ${error}`);
  159. }
  160. else
  161. {
  162. path = stdout.trim();
  163. console.log(song);
  164. console.log(path);
  165. dispatcher = connection.playFile(path);
  166. dispatcher.setVolume(0.75);
  167. dispatcher.setBitrate(2048);
  168. }
  169. });
  170. }
  171. });
  172. }
  173. });
  174. client.login(config.token);