Advertisement
Guest User

rebot code 2

a guest
Jun 26th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.07 KB | None | 0 0
  1. //Beep. Boop.
  2.  
  3. const Discord = require('discord.js');
  4. const client = new Discord.Client;
  5. const ytdl = require('ytdl-core');
  6. const request = require('request');
  7. const fs = require('fs')
  8. const getYouTubeID = require('get-youtube-id');
  9. const fetchVideoInfo = require('youtube-info')
  10. const yt_api_key = config.yt_api_key;
  11. const bot_controller = config.bot_controller;
  12.  
  13.  
  14. var queue = [];
  15. var isPlaying = false;
  16. var dispatcher = null;
  17. var voiceChannel = null;
  18. var skipReq = 0;
  19. var skippers = [];
  20.  
  21.  
  22.  
  23. function SetCurrentGame() {
  24. var presence = {};
  25. presence.game = {};
  26. presence.status = "online";
  27. presence.afk = false;
  28.  
  29. //What's the bot playing?
  30. switch (Math.floor(Math.random() * 1000) % 31) {
  31. case 0:
  32. presence.game.name = "nothing rn";
  33. break;
  34. case 1:
  35. presence.game.name = "r!help";
  36. break;
  37. case 2:
  38. presence.game.name = "with ban buttons";
  39. break;
  40. case 3:
  41. presence.game.name = "around";
  42. break;
  43. case 4:
  44. presence.game.name = "annoying lolrepeatlol™";
  45. break;
  46. case 5:
  47. presence.game.name = "something obviously";
  48. break;
  49. case 6:
  50. presence.game.name = "with the world";
  51. break;
  52. case 7:
  53. presence.game.name = "a game";
  54. break;
  55. case 8:
  56. presence.game.name = "with code";
  57. break;
  58. case 9:
  59. presence.game.name = "r!help for info";
  60. break;
  61. case 10:
  62. presence.game.name = "something";
  63. break;
  64. case 11:
  65. presence.game.name = "a movie";
  66. break;
  67. case 12:
  68. presence.game.name = "with my creator";
  69. break;
  70. case 13:
  71. presence.game.name = "the waiting game";
  72. break;
  73. case 14:
  74. presence.game.name = "with a giraffe";
  75. break;
  76. case 15:
  77. presence.game.name = "with best bird";
  78. break;
  79. case 16:
  80. presence.game.name = "with best squid";
  81. break;
  82. case 17:
  83. presence.game.name = "with jason";
  84. break;
  85. case 18:
  86. presence.game.name = "with happy magnets";
  87. break;
  88. case 19:
  89. presence.game.name = "with soy sauce";
  90. break;
  91. case 20:
  92. presence.game.name = "c:";
  93. break;
  94. case 21:
  95. presence.game.name = "taking over the world";
  96. break;
  97. case 22: //this is the point where i run out of ideas
  98. presence.game.name = "with boredom";
  99. break;
  100. case 23:
  101. presence.game.name = "on steam";
  102. break;
  103. case 24:
  104. presence.game.name = "(or rather, eating chick-fil-a?)";
  105. break;
  106. case 25:
  107. presence.game.name = "(or rather, fooling around?)";
  108. break;
  109. case 26:
  110. presence.game.name = "(or rather, doing nothing?)"
  111. break;
  112. case 27:
  113. presence.game.name = "munzee";
  114. break;
  115. case 28:
  116. presence.game.name = "geocaching";
  117. break;
  118. case 29:
  119. presence.game.name = "chess";
  120. break;
  121. case 30:
  122. presence.game.name = "reeeeeeeeeeeeeeee";
  123. break;
  124. }
  125. client.user.setPresence(presence);
  126. }
  127. function isYouTube(str) {
  128. return str.toLowerCase().indexOf('youtube.com') > -1;
  129. }
  130.  
  131. function search_video(query, callback) {
  132. request("https://www.googleapis.com/youtube/v3/search?part=id&type=video&q=" + encodeURIComponent(query) + "&key=" + yt_api_key, function(error, response, body) {
  133. var json = JSON.parse(body);
  134. callback(json.items[0].id.videoId);
  135. });
  136. }
  137. function getID(str, cb) {
  138. if (isYoutube(str)) {
  139. cb(getYouTubeID(str));
  140. } else {
  141. search_video(str, function(id) {
  142. cb(id);
  143. });
  144. }
  145. }
  146. function add_to_queue(strID) {
  147. if (isYouTube(strID)) {
  148. queue.push(getID(getYouTubeID(strID)));
  149. } else {
  150. queue.push(strID);
  151. }
  152. }
  153. function playMusic(id, message) {
  154. voiceChannel = message.member.voiceChannel;
  155.  
  156. voiceChannel.join().then(function (connection) {
  157. stream = ytdl("https://youtube.com/watch?v=" + id, {
  158. filter: "audioonly"
  159. })
  160. skipReq = 0;
  161. skippers = [];
  162.  
  163.  
  164. dispatcher = connection.playStream(stream);
  165. });
  166. }
  167.  
  168.  
  169. //Holds the prefix and token
  170. const config = JSON.parse(fs.readFileSync('./config.json', 'utf-8'));
  171. //Motivates me when turning bot on I guess
  172. client.on('ready', () => {
  173. console.log('go. destroy. create.');
  174. client.setInterval(SetCurrentGame, 150000);
  175. SetCurrentGame();
  176. });
  177.  
  178. //Greetings!
  179. client.on('guildMemberAdd', member => {
  180. let guild = member.guild;
  181. guild.defaultChannel.send(`oi ${member.user}! be sure to check out the rules! c:`);
  182. });
  183.  
  184. //Notifies when a guild is added in cmd.
  185. client.on("guildCreate", guild => {
  186. console.log(`new server added, woop woop!
  187. name: ${guild.name}
  188. owner: ${guild.owner.user.username}
  189. keep on working! c:`);
  190. });
  191.  
  192. client.on('message', message => {
  193. const ergs = message.content.split(" ").slice(1);
  194. if (message.author.bot) return;
  195. if (!message.content.startsWith(config.prefix)) return;
  196. //Prefix stuff
  197. let command = message.content.split(" ")[0];
  198. command = command.slice(config.prefix.length);
  199.  
  200. let args = message.content.split(" ").slice(1);
  201.  
  202.  
  203. //Math + command
  204. if (command === 'add') {
  205. let numArray = args.map(n=> parseInt(n));
  206. let total = numArray.reduce( (p, c) => p+c);
  207. message.channel.send(total);
  208. }
  209.  
  210. //Echo command
  211. if (command === 'say') {
  212. message.channel.send(args.join(" "));
  213. }
  214.  
  215. //<3
  216. if (command === 'ping') {
  217. message.channel.send('pong! c;');
  218. }
  219. if (command === 'pong') {
  220. message.channel.send('ping.', `you're not that funny.`)
  221. }
  222.  
  223. //Mod only easter-egg.
  224. if (command === `super`) {
  225. let modRole = message.guild.roles.find('name', 'moddi');
  226. if(message.member.roles.has(modRole.id)) {
  227. message.channel.send('secret c;');
  228. } else {
  229. message.reply(`bwahahaha you cant access the secrets of the ocean! ;p `);
  230. }
  231. }
  232.  
  233. //Kick members.
  234. if (command === 'kick') {
  235. let modRole = message.guild.roles.find("name", "Mod");
  236. if (!message.member.roles.has(modRole.id)) {
  237. message.channel.send(`sorry, you don't have permission to do that.`);
  238. }
  239. if (message.mentions.users.size === 0) {
  240. return message.channel.send(`don't forget to mention the user you want to kick!`);
  241. }
  242. let kickMember = message.guild.member(message.mentions.users.first());
  243. if(!kickMember) {
  244. return message.channel.send('wait, who? try retyping the username.');
  245. }
  246. if(!message.guild.member(client.user).hasPermission('KICK_MEMBERS')) {
  247. return message.channel.send(`i don't have the permission to do that. go into server settings > roles > reBot and give me the "Kick Members" permission. then, drag me over the user you'd like to kick. c:`)
  248. }
  249. kickMember.kick();
  250. }
  251.  
  252. //Ban members.
  253. if (command === `ban`) {
  254. let modRole = message.guild.roles.find("name", "Mod");
  255. if (!message.member.roles.has(modRole.id)) {
  256. message.channel.send(`sorry, you don't have permission to do that.`);
  257. }
  258. if (message.mentions.users.size === 0) {
  259. return message.channel.send(`don't forget to mention the user you want to ban!`);
  260. }
  261. let banMember = message.guild.member(message.mentions.users.first());
  262. if(!banMember) {
  263. return message.channel.send('wait, who? try retyping the username.');
  264. }
  265. if(!message.guild.member(client.user).hasPermission('BAN_MEMBERS')) {
  266. return message.channel.send(`i don't have the permission to do that. go into server settings > roles > reBot and give me the "Ban Members" permission. then, drag me over the user you'd like to ban. c:`)
  267. }
  268. banMember.ban().catch(console.error);
  269. }
  270.  
  271. if (command === `help`) {
  272. message.channel.send(`__** HELP **__
  273. --*ping*
  274. ping *the bot*
  275. pong *the bot*
  276. --*moderation*
  277. ban *someone*
  278. kick *someone*
  279. --*music*`
  280. );
  281. }
  282. //addrole and removerole. temp
  283. let user = message.author;
  284. if (command === "updates") {
  285. let addedRole = message.guild.roles.find("name", "Updates");
  286. message.guild.member(user).addRole(addedRole).catch(console.error);
  287. }
  288. //music!
  289. //play command
  290. if (command === "play") {
  291. if (queue.length > 0 || isPlaying) {
  292. getID(args, function (id) {
  293. add_to_queue(id);
  294. fetchVideoInfo(id, function (err, videoInfo){
  295. if (err) throw new Error(err);
  296. message.channel.send(':white_check_mark:**' + videoInfo.title + '** *was just added to the queue.*')
  297. });
  298. });
  299. } else {
  300. isPlaying = true;
  301. getID(args, function (id) {
  302. queue.push('placeholder');
  303. playMusic(id, message);
  304. fetchVideoInfo(id, function (err, videoInfo){
  305. if (err) throw new Error(err);
  306. message.channel.send(':white_check_mark: *now playing:* **' + videoInfo.title + '**')
  307. });
  308. });
  309. }
  310.  
  311. //The one and powerful eval command.
  312. if (command === 'eval') {
  313. if (message.author.id !== '271443182066204673') return;
  314. try {
  315. const code = args.join(" ");
  316. let evaled = eval(code);
  317.  
  318. if (typeof evaled !== "string")
  319. evaled = require("util").inspect(evaled);
  320.  
  321. message.channel.send(clean(evaled), {code:"xl"});
  322. } catch (err) {
  323. message.channel.send(`\`ERROR\` \`\`\`xl\n${clean(err)}\n\`\`\``);
  324. }
  325. }
  326. }
  327.  
  328. function clean(text) {
  329. if (typeof(text) === "string")
  330. return text.replace (/`/g, "`" + String.fromCharCode(8203)).replace(/@/g, "@" + String.fromCharCode(8203));
  331. else {
  332. return text;
  333. }
  334. }
  335.  
  336. //Told you so.
  337. client.login(config.token)
  338. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement