Unequealedbobparty

Untitled

Jan 29th, 2019
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. local groups = {}
  2. local module = {}
  3. local setmetatable = setmetatable
  4. local error = error
  5. local wait = wait
  6. local httpService = game:GetService'HttpService'
  7. local postAsync = httpService.PostAsync
  8. local getAsync = httpService.GetAsync
  9. local jsonEncode = httpService.JSONEncode
  10. local jsonDecode = httpService.JSONDecode
  11. local base, key
  12.  
  13. local function encode (tab)
  14. return jsonEncode(httpService, tab)
  15. end
  16.  
  17. local function decode (tab)
  18. return jsonDecode(httpService, tab)
  19. end
  20.  
  21. local function request (url, method, suppress, data)
  22. local body = method(httpService, url, data)
  23. local success, response = pcall(decode, body)
  24. local err
  25. if success then
  26. err = response.error
  27. else
  28. err = 'Response was not valid json, full body: '..body
  29. end
  30. if not err and suppress then
  31. return true, response
  32. elseif not err then
  33. return response
  34. elseif suppress then
  35. return false, err
  36. else
  37. error(err)
  38. end
  39. end
  40.  
  41. local http = {
  42. post = function (path, data, suppress)
  43. if not data then
  44. data = {}
  45. end
  46. data.key = key
  47. return request(base..path, postAsync, suppress, encode(data))
  48. end,
  49. get = function (path, suppress)
  50. return request(base..path, getAsync, suppress)
  51. end
  52. }
  53.  
  54. function groups.promote (group, userId)
  55. return http.post('/promote/'..group..'/'..userId)
  56. end
  57.  
  58. function groups.demote (group, userId)
  59. return http.post('/demote/'..group..'/'..userId)
  60. end
  61.  
  62. function groups.setRank (group, userId, rank)
  63. return http.post('/setRank/'..group..'/'..userId..'/'..rank)
  64. end
  65.  
  66. function groups.shout (group, message)
  67. return http.post('/shout/'..group, {message = message})
  68. end
  69.  
  70. function groups.post (group, message)
  71. return http.post('/post/'..group, {message = message})
  72. end
  73.  
  74. function groups.handleJoinRequest (group, username, accept)
  75. local acceptString = accept and 'true' or 'false'
  76. return http.post('/handleJoinRequest/'..group..'/'..username..'/'..acceptString)
  77. end
  78.  
  79. function groups.getPlayers (group, rank, limit, online)
  80. local job = http.post('/getPlayers/make/'..group..(rank and '/'..rank or '')..'?limit='..(limit and limit or '-2')..'&online='..(online and 'true' or 'false')).data.uid
  81. local complete, response = false
  82. repeat
  83. wait(0.5)
  84. local success
  85. success, response = http.get('/getPlayers/retrieve/'..job, true)
  86. if not success and response:match('$Response was not valid json') then
  87. error(response)
  88. elseif success then
  89. complete = response.data.complete
  90. end
  91. until complete
  92. return response
  93. end
  94.  
  95. function module.message (userId, subject, message)
  96. return http.post('/message/'..userId, {subject = subject, body = message})
  97. end
  98.  
  99. function module.forumPostNew (forumId, subject, body, locked)
  100. return http.post('/forumPost/new/'..forumId..'?locked='..(locked and 'true' or 'false'), {subject = subject, body = body})
  101. end
  102.  
  103. function module.forumPostReply (postId, body, locked)
  104. return http.post('/forumPost/reply/'..postId..'?locked='..(locked and 'true' or 'false'), {body = body})
  105. end
  106.  
  107. function module.getBlurb (userId)
  108. return http.get('/getBlurb/'..userId)
  109. end
  110.  
  111. return function (domain, newKey, group)
  112. local isString = (type(domain) == 'string')
  113. if (not domain) or (not isString) or (isString and #domain <= 0) then
  114. error('Url is required and must be a string greater than length 0')
  115. end
  116. isString = (type(newKey) == 'string')
  117. if (not newKey) or (not isString) or (isString and #newKey <= 0) then
  118. error('Key is required and must be a string greater than length 0')
  119. end
  120.  
  121. base = 'http://'..domain
  122. key = newKey
  123.  
  124. if group then
  125. local isNumber = (type(group) == 'number')
  126. if (not isNumber) or (group <= 0) then
  127. error('If group is provided it must be a number greater than 0')
  128. end
  129.  
  130. for name, func in next, groups do
  131. module[name] = function (...)
  132. return func(group, ...)
  133. end
  134. end
  135. return module
  136. end
  137.  
  138. for name, func in next, groups do
  139. module[name] = func
  140. end
  141. return module
  142. end
Add Comment
Please, Sign In to add comment