Advertisement
TheRockettek

Untitled

Apr 9th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. -- Store webhook token here so you do not have to enter it every time
  2. webhook_id = "300699421580984320"
  3. webhook_token = "7WqjfFDcVVyUfQZ8-S1WqxdBf7ZxeVHWLxdy8Joq6Wj2za-_dx_SZzp-b7aTC-3ZraT6"
  4. nicks = {}
  5.  
  6. http_header =
  7. {['ContentType'] = "application/json",
  8. }
  9.  
  10. function say(_tText)
  11. _tText = _tText or ""
  12. peripheral.call("left","say",_tText)
  13. end
  14.  
  15. if not fs.exists("nicks.tbl") then
  16. file = fs.open("nicks.tbl","w")
  17. file.write("{}")
  18. file.close()
  19. end
  20.  
  21. if not fs.exists("jsonapi") then
  22. shell.run("pastebin get 4nRg9CHU jsonapi")
  23. end
  24. os.loadAPI("jsonapi")
  25.  
  26. function hasNick(_sUser)
  27. if nicks[_sUser] then
  28. return true,nicks[_sUser]
  29. else
  30. return false,nil
  31. end
  32. end
  33.  
  34. function reload()
  35. file = fs.open("nicks.tbl","r")
  36. nickFile = textutils.unserialize(file.readAll())
  37. file.close()
  38. nicks = nickFile
  39. end
  40.  
  41. function flush()
  42. file = fs.open("nicks.tbl","w")
  43. file.write(textutils.serialize(nicks))
  44. file.close()
  45. end
  46.  
  47. if type(nicks) ~= "table" then
  48. print("The file went corrupt!")
  49. nicks = {}
  50. flush()
  51. reload()
  52. end
  53.  
  54. function http_post(_sURL,_sFORM)
  55. local httphandle = http.post("http://smaller.hol.es/proxy.php?url=" .. _sURL,_sFORM,http_header)
  56. local httpread = httphandle.readAll()
  57. httphandle.close()
  58. return httpread
  59. end
  60. function http_get(_sURL)
  61. local httphandle = http.get("http://smaller.hol.es/proxy.php?url=" .. _sURL,http_header)
  62. local httpread = httphandle.readAll()
  63. httphandle.close()
  64. return httpread
  65. end
  66.  
  67. if not webhook_id or #webhook_id ~= 18 then
  68. print("Webhook ID:")
  69. while true do
  70. webhook_id = io.read()
  71. if #webhook_id ~= 18 then
  72. print("Invalid bot id")
  73. else
  74. break
  75. end
  76. end
  77. end
  78.  
  79. if not webhook_token or #webhook_token ~= 68 then
  80. print("Webhook Token:")
  81. while true do
  82. webhook_token = io.read()
  83. if #webhook_token ~= 68 then
  84. print("Invalid bot token")
  85. else
  86. break
  87. end
  88. end
  89. end
  90.  
  91. print("Connecting...")
  92. webhook_info = jsonapi.decode(http_get("https://discordapp.com/api/webhooks/" .. webhook_id .. "/" .. webhook_token))
  93.  
  94. if type(webhook_info) ~= "table" then
  95. printError("A fatal error has occured!")
  96. printError(http_get("https://discordapp.com/api/webhooks/" .. webhook_id .. "/" .. webhook_token))
  97. return
  98. elseif webhook_info.code then
  99. print("An error occured:")
  100. print("Code: " .. webhook_info.code .. ": " .. webhook_info.message)
  101. return
  102. end
  103.  
  104. function discord_say(_sText) -- send message c:
  105. return http_post("https://discordapp.com/api/webhooks/" .. webhook_id .. "/" .. webhook_token,"content=" .. _sText)
  106. end
  107.  
  108. discord_say("***:ok_hand: DiscordBridge 1.0 :ok_hand:***")
  109. print("loaded")
  110. reload()
  111. while true do
  112. event,arg1,arg2,arg3 = os.pullEvent()
  113. if event == "chat_message" then
  114. if string.sub(arg2,1,1) == "+" then
  115. print("Command")
  116. local args = {}
  117. for word in arg2:gmatch("%w+") do table.insert(args,word) end
  118. if args[1] == "setnick" then
  119. if args[2] then
  120. nicks[arg1] = args[2]
  121. say("Set " .. arg1 .. "'s nick to '" .. args[2] .. "'")
  122. discord_say("Set **" .. arg1 .. "**'s nick to " .. args[2])
  123. print("Set " .. arg1 .. " nick to " .. args[2])
  124. flush()
  125. reload()
  126. end
  127. end
  128. else
  129. gotNick,nick = hasNick(arg1)
  130. if gotNick then
  131. arg1 = nick
  132. end
  133. print(arg1 .. ": " .. arg2)
  134. discord_say("**" .. arg1 .. "**: " .. arg2)
  135. end
  136. end
  137. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement