Advertisement
sonntt

Untitled

Apr 2nd, 2022
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. const {
  2. CommandInteraction,
  3. MessageEmbed
  4. } = require("discord.js");
  5. const axios = require('axios');
  6.  
  7. module.exports = {
  8. name: "weather",
  9. description: "Displays/Forecasts the weather statistics",
  10. options: [
  11. {
  12. name: 'mode',
  13. description: 'Forecast/current',
  14. type: 'STRING',
  15. required: true,
  16. choices: [
  17. {
  18. name: 'forecast',
  19. value: 'forecast'
  20. },
  21.  
  22. {
  23. name: 'current',
  24. value: 'current'
  25. }
  26. ]
  27. },
  28.  
  29. {
  30. name: 'location',
  31. description: 'Location',
  32. type: 'STRING',
  33. required: true
  34. }
  35. ],
  36.  
  37. /**
  38. *
  39. * @param {CommandInteraction} interaction
  40. */
  41. async execute(interaction) {
  42. const {
  43. options
  44. } = interaction
  45.  
  46. switch (options.get('mode').value) {
  47. case 'forecast': {
  48. return fetchReply('forecast', interaction, options)
  49. }
  50.  
  51. case 'current': {
  52. return fetchReply('current', interaction, options)
  53. }
  54. }
  55. }
  56. }
  57.  
  58. function fetchReply(type, interaction, options) {
  59. let url = `https://api.weatherapi.com/v1/${type}.json?key=${process.env.WEATHER_API_KEY}&q=${options.get('location').value}`
  60.  
  61. if (type == 'forecast') {
  62. url += '&days=1&aqi=no&alerts=no'
  63. } else {
  64. url += '&aqi=no'
  65. }
  66.  
  67. console.log(url)
  68. axios.get(url).then(response => {
  69. const data = response.data
  70. var displayData = {}
  71.  
  72. console.log(data)
  73. if (type == 'current') {
  74. displayData = {
  75. locationName: data.location.name,
  76. lastUpdated: data.current.last_updated,
  77. thumbnail: `https://${data.current.condition.icon.slice(2, data.current.condition.icon.length)}`,
  78. generalStatus: data.current.condition.text,
  79. temperature: `${data.current.temp_c}°C`,
  80. wind: `${data.current.wind_kph} kph, ${data.current.wind_dir}`,
  81. cloud: `${response.data.current.cloud}%`,
  82. humidity: `${response.data.current.humidity}%`,
  83. description: `Current's weather statistics for ${data.location.name} (${data.current.last_updated})`
  84. }
  85. } else {
  86. displayData = {
  87. locationName: data.location.name,
  88. lastUpdated: data.forecast.forecastday.date,
  89. temps: `${data.forecast.forecastday.avgtemp_c}°C`,
  90. visibility: `${data.forecast.forecastday.avgvis_km} km`,
  91. humidity: `${JSON.stringify(data.forecast.forecastday).avghumidity}%`,
  92. generalStatus: JSON.stringify(data.forecast.forecastday).condition.text,
  93. thumbnail: `https://${JSON.stringify(data.forecast.forecastday).condition.icon.slice(2, data.forecast.forecastday.condition.icon.length)}`,
  94. description: `Weather forecast at ${data.location.name} for tomorrow (${JSON.stringify(data.forecast.forecastday).date})`
  95. }
  96. }
  97.  
  98. console.log(displayData)
  99. let embed = new MessageEmbed()
  100. .setTitle('🌦 Weather')
  101. .setColor('GREEN')
  102. .setFooter({
  103. text: `Requested by ${interaction.user.username}`,
  104. iconURL: interaction.user.avatarURL(true)
  105. })
  106. .setTimestamp()
  107. .setThumbnail(displayData.thumbnail)
  108. .setDescription(displayData.description)
  109.  
  110. if (type == 'forecast') {
  111. embed.addFields(
  112. {
  113. name: 'General status',
  114. value: displayData.generalStatus,
  115. inline: true
  116. },
  117.  
  118. {
  119. name: 'Temperature',
  120. value: displayData.temps,
  121. inline: true
  122. },
  123.  
  124. {
  125. name: 'Visibility',
  126. value: displayData.visibility,
  127. inline: true
  128. },
  129.  
  130. {
  131. name: 'Humidity',
  132. value: displayData.humidity,
  133. inline: true
  134. }
  135. )
  136. } else {
  137. embed.addFields(
  138. {
  139. name: 'General status',
  140. value: displayData.generalStatus,
  141. inline: true
  142. },
  143.  
  144. {
  145. name: 'Temperature',
  146. value: displayData.temperature,
  147. inline: true
  148. },
  149.  
  150. {
  151. name: "\u200B",
  152. value: '\u200B',
  153. inline: false
  154. },
  155.  
  156. {
  157. name: 'Wind',
  158. value: displayData.wind,
  159. inline: true
  160. },
  161.  
  162. {
  163. name: 'Cloud',
  164. value: displayData.cloud,
  165. inline: true
  166. },
  167.  
  168. {
  169. name: 'Humidity',
  170. value: displayData.humidity,
  171. inline: true
  172. }
  173. )
  174. }
  175.  
  176. interaction.reply({
  177. embeds: [
  178. embed
  179. ]
  180. })
  181. })
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement