Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 KB | None | 0 0
  1. 'use strict'
  2.  
  3. const Discord = require('discord.js')
  4. const Rcon = require('./lib/rcon.js')
  5. const express = require('express')
  6. const axios = require('axios')
  7. const emojiStrip = require('emoji-strip')
  8. const { Tail } = require('tail')
  9. const fs = require('fs')
  10.  
  11. const configFile = (process.argv.length > 2) ? process.argv[2] : './config.json'
  12.  
  13. console.log('[INFO] Using configuration file:', configFile)
  14.  
  15. const c = require(configFile)
  16.  
  17. let app = null
  18. let tail = null
  19.  
  20. function fixUsername (username) {
  21. return username.replace(/(§[A-Z-a-z0-9])/g, '')
  22. }
  23.  
  24. // replace mentions with discriminator with the actual mention
  25. function replaceDiscordMentions (message) {
  26. if (c.ALLOW_USER_MENTIONS) {
  27. const possibleMentions = message.match(/@(\S+)/gim)
  28. if (possibleMentions) {
  29. for (let mention of possibleMentions) {
  30. const mentionParts = mention.split('#')
  31. let username = mentionParts[0].replace('@', '')
  32. if (mentionParts.length > 1) {
  33. const user = shulker.users.find(user => user.username === username && user.discriminator === mentionParts[1])
  34. if (user) {
  35. message = message.replace(mention, '<@' + user.id + '>')
  36. }
  37. }
  38. }
  39. }
  40. }
  41. return message
  42. }
  43.  
  44. function makeDiscordMessage (username, message) {
  45. // make a discord message string by formatting the configured template with the given parameters
  46. message = replaceDiscordMentions(message)
  47.  
  48. return c.DISCORD_MESSAGE_TEMPLATE
  49. .replace('%username%', username)
  50. .replace('%message%', message)
  51. }
  52.  
  53. function makeDiscordWebhook (username, message) {
  54. message = replaceDiscordMentions(message)
  55.  
  56. return {
  57. username: username,
  58. content: message,
  59. 'avatar_url': `https://minotar.net/helm/${username}/256.png`
  60. }
  61. }
  62.  
  63. function makeMinecraftTellraw (message) {
  64. // same as the discord side but with discord message parameters
  65. const username = emojiStrip(message.author.username)
  66. const discriminator = message.author.discriminator
  67. const text = emojiStrip(message.cleanContent)
  68. // hastily use JSON to encode the strings
  69. const variables = JSON.parse(JSON.stringify({ username, discriminator, text }))
  70.  
  71. return c.MINECRAFT_TELLRAW_TEMPLATE
  72. .replace('%username%', variables.username)
  73. .replace('%discriminator%', variables.discriminator)
  74. .replace('%message%', variables.text)
  75. }
  76.  
  77. const debug = c.DEBUG
  78. const shulker = new Discord.Client()
  79.  
  80. function initApp () {
  81. // run a server if not local
  82. if (!c.IS_LOCAL_FILE) {
  83. app = express()
  84. const http = require('http').Server(app)
  85.  
  86. app.use(function (request, response, next) {
  87. request.rawBody = ''
  88. request.setEncoding('utf8')
  89.  
  90. request.on('data', function (chunk) {
  91. request.rawBody += chunk
  92. })
  93.  
  94. request.on('end', function () {
  95. next()
  96. })
  97. })
  98.  
  99. const serverport = process.env.PORT || c.PORT
  100.  
  101. http.listen(serverport, function () {
  102. console.log('[INFO] Bot listening on *:' + serverport)
  103. })
  104. } else {
  105. if (fs.existsSync(c.LOCAL_FILE_PATH)) {
  106. console.log('[INFO] Using configuration for local file at "' + c.LOCAL_FILE_PATH + '"')
  107. tail = new Tail(c.LOCAL_FILE_PATH)
  108. } else {
  109. throw new Error('[ERROR] Local file not found at "' + c.LOCAL_FILE_PATH + '"')
  110. }
  111. }
  112. }
  113.  
  114. function watch (callback) {
  115. if (c.IS_LOCAL_FILE) {
  116. tail.on('line', function (data) {
  117. // ensure that this is a message
  118. if (data.indexOf(': <') !== -1) {
  119. callback(data)
  120. }
  121. })
  122. } else {
  123. app.post(c.WEBHOOK, function (request, response) {
  124. callback(request.rawBody)
  125. response.send('')
  126. })
  127. }
  128. }
  129.  
  130. shulker.on('ready', function () {
  131. watch(function (body) {
  132. console.log('[INFO] Recieved ' + body)
  133. const re = new RegExp(c.REGEX_MATCH_CHAT_MC)
  134. const ignored = new RegExp(c.REGEX_IGNORED_CHAT)
  135. if (!ignored.test(body)) {
  136. const bodymatch = body.match(re)
  137. const username = fixUsername(bodymatch[1])
  138. const message = bodymatch[2]
  139. if (debug) {
  140. console.log('[DEBUG] Username: ' + bodymatch[1])
  141. console.log('[DEBUG] Text: ' + bodymatch[2])
  142. }
  143. if (c.USE_WEBHOOKS) {
  144. const webhook = makeDiscordWebhook(username, message)
  145. axios.post(c.WEBHOOK_URL, {
  146. ...webhook
  147. }, {
  148. headers: {
  149. 'Content-Type': 'application/json'
  150. }
  151. })
  152. } else {
  153. // find the channel
  154. const channel = shulker.channels.find((ch) => ch.id === c.DISCORD_CHANNEL_ID && ch.type === 'text')
  155. channel.send(makeDiscordMessage(username, message))
  156. }
  157. }
  158. })
  159. })
  160.  
  161. shulker.on('message', function (message) {
  162. if (message.channel.id === c.DISCORD_CHANNEL_ID && message.channel.type === 'text') {
  163. if (c.USE_WEBHOOKS && message.webhookID) {
  164. return // ignore webhooks if using a webhook
  165. }
  166. if (message.author.id !== shulker.user.id) {
  167. if (message.attachments.length) { // skip images/attachments
  168. return
  169. }
  170. const client = new Rcon(c.MINECRAFT_SERVER_RCON_IP, c.MINECRAFT_SERVER_RCON_PORT) // create rcon client
  171. client.auth(c.MINECRAFT_SERVER_RCON_PASSWORD, function () { // only authenticate when needed
  172. client.command('tellraw @a ' + makeMinecraftTellraw(message), function (err) {
  173. if (err) {
  174. console.log('[ERROR]', err)
  175. }
  176. client.close() // close the rcon connection
  177. })
  178. })
  179. }
  180. }
  181. })
  182.  
  183. initApp()
  184. shulker.login(c.DISCORD_TOKEN)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement