Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.02 KB | None | 0 0
  1. const Discord = require('discord.js')
  2. const mineflayer = require('mineflayer')
  3. const fetch = require('node-fetch')
  4. const credentials = require("./settings/credentials.json")
  5. const config = require("./settings/config.json")
  6. const embeds = require("./modules/sendEmbeds")
  7. const client = new Discord.Client()
  8.  
  9. var botOnline = false
  10. var botLockDown = true
  11.  
  12. client.on("ready", () => {
  13. console.log(`[D] - Logged in as ${client.user.tag}`)
  14. client.user.setStatus('idle', 'Idling')
  15. })
  16.  
  17. client.on("message", async message => {
  18. if (message.author.bot) return
  19. if (message.channel.type !== "text") return
  20. if (message.content.toLowerCase() === config.prefix) {
  21. message.delete()
  22. embeds.sendMainHelpEmbed(message.channel, message.author)
  23. return
  24. } else {
  25. if (message.content.startsWith(config.prefix)) {
  26. message.delete()
  27. var args = message.content.split(" ").slice(1)
  28. switch (args[0].toLowerCase()) {
  29. case "help":
  30. if (!args[1]) {
  31. embeds.sendMainHelpEmbed(message.channel, message.author)
  32. return
  33. } else {
  34. switch (args[1].toLowerCase()) {
  35. case "start":
  36. embeds.sendSingleHelpEmbed(message.channel, message.author, "Start", "/housing start", '/housing start', 'Housing')
  37. break
  38. case "stop":
  39. embeds.sendSingleHelpEmbed(message.channel, message.author, "Stop", "/housing stop", '/housing stop', 'Housing')
  40. break
  41. case "restart":
  42. embeds.sendSingleHelpEmbed(message.channel, message.author, 'Restart', '/housing restart', '/housing restart', 'Housing')
  43. case "say":
  44. embeds.sendSingleHelpEmbed(message.channel, message.author, "Say", "/housing say <message>", '/housing say monkaS', 'Karaoke Leaders')
  45. break
  46. case "privacy":
  47. embeds.sendSingleHelpEmbed(message.channel, message.author, "Privacy", "/housing privacy <private | party | public>", '/housing privacy public', 'Event Advisers')
  48. break
  49. case "invite":
  50. embeds.sendSingleHelpEmbed(message.channel, message.author, "Invite", "/housing invite <username>", '/housing invite FunnyFungi', 'Housing')
  51. break
  52. default:
  53. embeds.sendMainHelpEmbed(message.channel, message.author)
  54. }
  55. }
  56. break
  57. case "start":
  58. if (botOnline === false) {
  59. createBot()
  60. global.workingEmbed = await embeds.sendEmbed('working', message.channel, 'Starting the housing bot . . .', message.author, null)
  61. setTimeout(function() {
  62. bot.chat("/home")
  63. }, 5 * 1000)
  64. botOnline = true
  65. } else {
  66. embeds.sendEmbed('error', message.channel, 'Housing bot already in housing', message.author, 5)
  67. return
  68. }
  69. break
  70. case "stop":
  71. if (botOnline === false) {
  72. embeds.sendEmbed('error', message.channel, 'Housing bot already offline', message.author, 5)
  73. return
  74. } else {
  75. embeds.sendEmbed('success', message.channel, 'Housing bot logged off', message.author, null)
  76. botOnline = false
  77. killBot()
  78. }
  79. break
  80. case "restart":
  81. if (botOnline === false) {
  82. embeds.sendEmbed('error', message.channel, 'Housing bot offline', message.author, 3)
  83. return
  84. } else {
  85. killBot()
  86. var restartEmbed = await embeds.sendEmbed('working', message.channel, 'Restarting housing bot . . . ', message.author, null)
  87. setTimeout(function() {
  88. embeds.editEmbed('success', restartEmbed, 'Restarted housing bot, logging back in', message.author, null)
  89. createBot()
  90. }, 3 * 1000)
  91. }
  92. break
  93. case "privacy":
  94. if (botOnline === false) {
  95. embeds.sendEmbed('error', message.channel, 'Housing bot offline', message.author, 3)
  96. return
  97. } else {
  98. if (botLockDown === true) {
  99. embeds.sendEmbed('error', message.channel, 'Housing bot not in Housing', message.author, 4)
  100. return
  101. } else {
  102. if (!args[1]) {
  103. embeds.sendEmbed('error', message.channel, 'Try `PUBLIC`, `PARTY` or `PRIVATE`', message.author, 5)
  104. return
  105. } else {
  106. if (!message.member.roles.has(message.guild.roles.find(role => role.name === config.roles.advisers)) && !message.member.roles.has(message.guild.roles.find(role => role.name === config.roles.leaders))) {
  107. embeds.sendEmbed('error', message.channel, 'You need the role \`EVENT ADVISERS\` to run that command', message.author, 5)
  108. return
  109. } else {
  110. switch (args[1].toLowerCase()) {
  111. case "party":
  112. bot.chat("/visitingrule PARTY")
  113. embeds.sendEmbed('success', message.channel, 'The housing is now set to **PARTY** - Only party members can join', message.author, null)
  114. break
  115. case "public":
  116. bot.chat("/visitingrule PUBLIC")
  117. embeds.sendEmbed('success', message.channel, 'The housing is now set to **PUBLIC** - Anyone can join', message.author, null)
  118. break
  119. case "private":
  120. bot.chat('/visitingrule PRIVATE')
  121. embeds.sendEmbed('success', message.channel, 'The housing is now set to **PRIVATE** - No one can join', message.author, null)
  122. break
  123. default:
  124. embeds.sendEmbed('error', message.channel, 'Try `PUBLIC`, `PARTY` or `PRIVATE`', message.author, 5)
  125. }
  126. }
  127. }
  128. }
  129. }
  130. break
  131. case "invite":
  132. if (botOnline === false) {
  133. embeds.sendEmbed('error', message.channel, 'Housing bot offline', message.author, 3)
  134. return
  135. } else {
  136. if (!args[1]) return embeds.sendEmbed('error', message.channel, 'You haven\'t supplied a username to invite', message.author, 5)
  137. var personToInvite = args[1]
  138. if (personToInvite.match("[A-Za-z0-9_]")) {
  139. if (personToInvite === "HypixelKaraoke") {
  140. embeds.sendEmbed('error', message.channel, 'You can\'t invite me to my own house ', message.author, 5)
  141. return
  142. }
  143. var inviteEmbed = await embeds.sendEmbed('working', message.channel, `Attempting to invite \`${personToInvite}\` to the plot`, message.author, null)
  144. bot.chat('/p invite ' + personToInvite)
  145. bot.on("message", msg => {
  146. var msgToString = msg.toString().toLowerCase()
  147. if (msgToString.startsWith("can't find a player by the name of '" + personToInvite.toLowerCase() +"'")) {
  148. embeds.editEmbed('error', inviteEmbed, `Can't find a player by the name of \`${personToInvite}\``, message.author, 7)
  149. return
  150. } else if (msgToString.startsWith("you can't invite") && msgToString.endsWith(personToInvite.toLowerCase() + " to the party because they're not online!")) {
  151. embeds.editEmbed('error', inviteEmbed, `Can't invite \`${personToInvite}\` to the party because they're not online`, message.author, 7)
  152. return
  153. } else if (msgToString.startsWith("[vip] apskull invited") && msgToString.endsWith(personToInvite.toLowerCase() + " to the party! they have 60 seconds to accept.")) {
  154. embeds.editEmbed('success', inviteEmbed, `Invited \`${personToInvite}\` to the party`, message.author, null)
  155. }
  156. })
  157. } else {
  158. embeds.editEmbed('error', inviteEmbed, `\`${personToInvite}\` is not a valid username`, message.author, 5)
  159. return
  160. }
  161. }
  162. break
  163. case "say":
  164. if (botOnline === false) {
  165. embeds.sendEmbed('error', message.channel, 'Housing bot offline', message.author, 3)
  166. return
  167. } else {
  168. if (!message.member.roles.has(message.guild.roles.find(role => role.name === config.roles.leaders))) {
  169. embeds.sendEmbed('error', message.channel, 'You need the role \`KARAOKE LEADERS\` to run that command', message.author, 5)
  170. return
  171. } else {
  172. if (args[1].toLowerCase() === "/resetplot" || args[1].toLowerCase() === "/settheme") {
  173. embeds.sendEmbed('error', message.channel, "This command cannot be run from the bot", message.author, 5)
  174. return
  175. } else {
  176. if (args.slice(1).length === 0) {
  177. embeds.sendEmbed('error', message.channel, "Please supply a message to send as the housing bot", message.author, 5)
  178. return
  179. } else {
  180. embeds.sendEmbed('success', message.channel, `Sending chat message \`${args.slice(1).join(" ")}\` as the housing bot`, message.author, null)
  181. bot.chat(args.slice(1).join(" "))
  182. }
  183. }
  184. }
  185. }
  186. break
  187. default:
  188. embeds.sendMainHelpEmbed(message.channel, message.author)
  189. }
  190. }
  191. }
  192. })
  193.  
  194. function urlcustomencode(str) {
  195. var symbols = {
  196. '@': '%40',
  197. '*': '%2A',
  198. '+': '%2B',
  199. '/': '%2F',
  200. '%26lt%3B': '%3C',
  201. '%26gt%3B': '%3E'
  202. }
  203. return escape(str).replace(/([@*+/]|%26(amp|lt|gt)%3B)/g, function(m) {
  204. return symbols[m]
  205. })
  206. }
  207.  
  208.  
  209. client.login(credentials.botToken).catch(err => console.error(err))
  210.  
  211. function createBot() {
  212. client.user.setStatus('online', 'Chilling in Housing')
  213. try {
  214. bot = mineflayer.createBot({
  215. host: "mc.hypixel.net",
  216. port: 25565,
  217. version: '1.9',
  218. username: credentials.username,
  219. password: credentials.password
  220. })
  221.  
  222. bot.on('error', err => {
  223. console.error(err)
  224. embeds.sendEmbed('error', client.guilds.find(guild => guild.name === config.guild).channels.find(channel => channel.name === config.channels.housingCommands), "Minecraft bot failed to connect, check console for details", null, null)
  225. killBot()
  226. return
  227. })
  228.  
  229. bot.on('message', message => {
  230. if (bot == null) return
  231. let plainText = message.toString()
  232. let colourText = message.toMotd()
  233. if (plainText.match(new RegExp("^(?:\\[[A-Z]+\\+?\\] ?)?" + bot.username + " entered the world\.$"))) {
  234. bot.look((Math.PI), 0, false)
  235. bot.setControlState("back", true)
  236. setTimeout(() => bot.setControlState("back", false), 1000)
  237. } else if (plainText.toLowerCase() === "welcome to the housing beta!") {
  238. embeds.editEmbed('success', workingEmbed, 'Housing bot logged into housing', message.author, null)
  239. botLockDown = false
  240. } else if (plainText.includes(":") || plainText.includes('entered the world') || plainText.includes('left the world')) {
  241. // fetch('https://kipli.ng/karaoke/submithousingchat', { method: 'POST', body: `key=${credentials.kiplingAPIKey}&message=${urlcustomencode(colourText)}`})
  242. // .then(res => console.log(res))
  243. } else {
  244. if (plainText.toLowerCase().startsWith("you are currently vanished")) {
  245. botLockDown = true
  246. return
  247. }
  248. client.guilds.find(guild => guild.name === config.guild).channels.find(channel => channel.name === config.channels.housingLog).send('`' + plainText + '`')
  249. }
  250. })
  251.  
  252. var homeInterval = setInterval(function() {
  253. if (bot == null) {
  254. clearInterval(homeInterval)
  255. } else {
  256. bot.chat("/home")
  257. }
  258. }, 15000)
  259. } catch (e) {
  260. console.error(e)
  261. embeds.sendEmbed('error', client.guilds.find(guild => guild.name === config.guild).channels.find(channel => channel.name === config.channels.housingCommands), "Minecraft bot failed to connect, check console for details", null, null)
  262. killBot()
  263. }
  264. }
  265.  
  266. var macroString
  267. var macroTimeoutMs = 10000
  268. var macroTimeout = createTimeout(macroTimeoutMs)
  269.  
  270. function createTimeout(ms) {
  271. return setTimeout(function(){
  272. kiplingMacroRequest()
  273. }, parseInt(ms))
  274. }
  275.  
  276. function killBot() {
  277. if (bot == null) {
  278. botOnline = false
  279. botLockDown = true
  280. } else {
  281. bot.quit()
  282. bot = null
  283. botOnline = false
  284. botLockDown = true
  285. client.user.setStatus('idle', 'Idling')
  286. }
  287. }
  288.  
  289. function mcChat(macroString) {
  290. if (botOnline === true && bot !== null && botLockDown !== true) {
  291. bot.chat(macroString)
  292. }
  293. }
  294.  
  295. function kiplingMacroRequest() {
  296. fetch("https://kipli.ng/api/karaoke/housingmacro")
  297. .then(res => res.text())
  298. .then(body => {
  299. if (!body.startsWith("/")) {
  300. mcChat(body)
  301. body = body
  302. } else {
  303. embeds.sendEmbed('warning', client.guilds.find(guild => guild.name === config.guild).channels.find(channel => channel.name === config.channels.housingCommands), "A macro sourced from Kipling was a command", null, null)
  304. return
  305. }
  306. })
  307. fetch("https://kipli.ng/api/karaoke/housingmacrointerval")
  308. .then(res => res.text())
  309. .then(body => {
  310. macroTimeout = createTimeout(body)
  311. })
  312. }
  313.  
  314.  
  315. // function kiplingNotifications() {
  316. // if(housingState == true){
  317. // https.get("KIPLING_LINK_REVOKED", response => {
  318. // var notification = ''
  319. // response.setEncoding('utf8');
  320. // response.on('data', function(d) {
  321. // notification = d
  322. // })
  323. // response.on('end', function() {
  324. // if(notification.startsWith("/resetplot") || notification.startsWith("/settheme") || notification.startsWith("resetparkour")) {
  325. // discordBot.channels.get("534109440165675009").send(discordBot.emojis.get("544943638157852695") + " A chat message sourced from Kipling was a command that is banned.")
  326. // mcChat(notification)
  327. // } else {
  328. // mcChat(notification)
  329. // }
  330. // })
  331. // })
  332. // }
  333. // }
  334.  
  335. // setInterval(kiplingNotifications, 3000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement