Advertisement
Bolodefchoco_LUAXML

Covid Tool

Mar 10th, 2020
995
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.95 KB | None | 0 0
  1. local head, body = discord.http("https://www.worldometers.info/coronavirus/")
  2. if head.code ~= 200 then
  3.     return discord.sendError("COVID", "Failed to retrieve data", "Try again later.")
  4. end
  5.  
  6. if not parameters then
  7.     local remCommaFromNum = function(str)
  8.         return tonumber((str:gsub(',', '')))
  9.     end
  10.  
  11.     local mainCounter = string.gmatch(body, "maincounter%-number\".->([%d,]+)")
  12.     local totalCases = mainCounter()
  13.  
  14.     local totalDeaths = mainCounter()
  15.     local totalRecovered = mainCounter()
  16.     local closedCases = remCommaFromNum(totalDeaths) + remCommaFromNum(totalRecovered)
  17.  
  18.     local activeCasesInMildCondition = string.match(body, "#8080FF\">([%d,]+)</span>.+Mild Condition")
  19.     local activeCasesInCriticalCondition = string.match(body, ":red \">([%d,]+)</span>.+Serious or Critical")
  20.     local activeCases = remCommaFromNum(activeCasesInMildCondition) + remCommaFromNum(activeCasesInCriticalCondition)
  21.  
  22.     discord.reply({
  23.         embed = {
  24.             color = 0xAD0000,
  25.             title = "COVID-19 Outbreak",
  26.             fields = {
  27.                 {
  28.                     name = "😷 Total Cases",
  29.                     value = totalCases,
  30.                     inline = false
  31.                 },
  32.                 {
  33.                     name = "💀 Total Deaths",
  34.                     value = totalDeaths,
  35.                     inline = true
  36.                 },
  37.                 {
  38.                     name = ":green_heart: Total Recovered",
  39.                     value = totalRecovered,
  40.                     inline = true
  41.                 },
  42.                 {
  43.                     name = "↗️ Active Cases",
  44.                     value = activeCases,
  45.                     inline = false
  46.                 },
  47.                 {
  48.                     name = "👁️ Cases in Mild Condition",
  49.                     value = activeCasesInMildCondition,
  50.                     inline = true
  51.                 },
  52.                 {
  53.                     name = "⚠️ Cases in Critical Condition",
  54.                     value = activeCasesInCriticalCondition,
  55.                     inline = true
  56.                 }
  57.             }
  58.         }
  59.     })
  60. else
  61.     if #parameters < 2 then
  62.         return discord.sendError("COVID", "Invalid parameter.", "Use the country name in English, not its abbreviation.")
  63.     end
  64.  
  65.     local defaultZero = function(str)
  66.         return ((str == string.rep(' ', #str)) and 0 or str)
  67.     end
  68.  
  69.     local i, f = body:find("<table .+</table>")
  70.     if not i then
  71.         return discord.sendError("COVID", "Error.", "Try again.")
  72.     end
  73.     body = body:sub(i, f)
  74.     local data = body:lower():match("%W" .. parameters:lower() .. ".+</tr>")
  75.     if not data then
  76.         return print("Country not found.")
  77.     end
  78.     data = data:gmatch("<td.->(.-)</td>")
  79.  
  80.     local totalCases = defaultZero(data())
  81.     --data()
  82.     local newCases = defaultZero(data())
  83.     local totalDeaths = defaultZero(data())
  84.     local newDeaths = defaultZero(data())
  85.     local totalRecovered = defaultZero(data())
  86.     --data()
  87.     local activeCases = defaultZero(data())
  88.     local seriousCritical = defaultZero(data())
  89.     --data()
  90.     local totalCasesPerMillion = defaultZero(data())
  91.  
  92.     discord.reply({
  93.         embed = {
  94.             color = 0xAD0000,
  95.             title = "'" .. parameters .. "' COVID-19 Outbreak",
  96.             fields = {
  97.                 {
  98.                     name = ":mask: Total Cases",
  99.                     value = totalCases,
  100.                     inline = true
  101.                 },
  102.                 {
  103.                     name = ":mask: New Cases",
  104.                     value = newCases,
  105.                     inline = true
  106.                 },
  107.                 {
  108.                     name = ":family_mwgb: Total Cases Per Million People",
  109.                     value = totalCasesPerMillion,
  110.                     inline = false
  111.                 },
  112.                 {
  113.                     name = ":green_heart: Total Recovered",
  114.                     value = totalRecovered,
  115.                     inline = false
  116.                 },
  117.                 {
  118.                     name = ":skull: Total Deaths",
  119.                     value = totalDeaths,
  120.                     inline = true
  121.                 },
  122.                 {
  123.                     name = ":coffin: New Deaths",
  124.                     value = newDeaths,
  125.                     inline = true
  126.                 },
  127.                 { name = '‌', value = '‌', inline = true },
  128.                 {
  129.                     name = ":eye: Active Cases",
  130.                     value = activeCases,
  131.                     inline = true
  132.                 },
  133.                 {
  134.                     name = ":warning: Serious/Critical Cases",
  135.                     value = seriousCritical,
  136.                     inline = true
  137.                 }
  138.             }
  139.         }
  140.     })
  141. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement