Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const {
- CommandInteraction,
- MessageEmbed
- } = require("discord.js");
- const axios = require('axios');
- module.exports = {
- name: "weather",
- description: "Displays/Forecasts the weather statistics",
- options: [
- {
- name: 'mode',
- description: 'Forecast/current',
- type: 'STRING',
- required: true,
- choices: [
- {
- name: 'forecast',
- value: 'forecast'
- },
- {
- name: 'current',
- value: 'current'
- }
- ]
- },
- {
- name: 'location',
- description: 'Location',
- type: 'STRING',
- required: true
- }
- ],
- /**
- *
- * @param {CommandInteraction} interaction
- */
- async execute(interaction) {
- const {
- options
- } = interaction
- switch (options.get('mode').value) {
- case 'forecast': {
- return fetchReply('forecast', interaction, options)
- }
- case 'current': {
- return fetchReply('current', interaction, options)
- }
- }
- }
- }
- function fetchReply(type, interaction, options) {
- let url = `https://api.weatherapi.com/v1/${type}.json?key=${process.env.WEATHER_API_KEY}&q=${options.get('location').value}`
- if (type == 'forecast') {
- url += '&days=1&aqi=no&alerts=no'
- } else {
- url += '&aqi=no'
- }
- console.log(url)
- axios.get(url).then(response => {
- const data = response.data
- var displayData = {}
- console.log(data)
- if (type == 'current') {
- displayData = {
- locationName: data.location.name,
- lastUpdated: data.current.last_updated,
- thumbnail: `https://${data.current.condition.icon.slice(2, data.current.condition.icon.length)}`,
- generalStatus: data.current.condition.text,
- temperature: `${data.current.temp_c}°C`,
- wind: `${data.current.wind_kph} kph, ${data.current.wind_dir}`,
- cloud: `${response.data.current.cloud}%`,
- humidity: `${response.data.current.humidity}%`,
- description: `Current's weather statistics for ${data.location.name} (${data.current.last_updated})`
- }
- } else {
- displayData = {
- locationName: data.location.name,
- lastUpdated: data.forecast.forecastday.date,
- temps: `${data.forecast.forecastday.avgtemp_c}°C`,
- visibility: `${data.forecast.forecastday.avgvis_km} km`,
- humidity: `${JSON.stringify(data.forecast.forecastday).avghumidity}%`,
- generalStatus: JSON.stringify(data.forecast.forecastday).condition.text,
- thumbnail: `https://${JSON.stringify(data.forecast.forecastday).condition.icon.slice(2, data.forecast.forecastday.condition.icon.length)}`,
- description: `Weather forecast at ${data.location.name} for tomorrow (${JSON.stringify(data.forecast.forecastday).date})`
- }
- }
- console.log(displayData)
- let embed = new MessageEmbed()
- .setTitle('🌦 Weather')
- .setColor('GREEN')
- .setFooter({
- text: `Requested by ${interaction.user.username}`,
- iconURL: interaction.user.avatarURL(true)
- })
- .setTimestamp()
- .setThumbnail(displayData.thumbnail)
- .setDescription(displayData.description)
- if (type == 'forecast') {
- embed.addFields(
- {
- name: 'General status',
- value: displayData.generalStatus,
- inline: true
- },
- {
- name: 'Temperature',
- value: displayData.temps,
- inline: true
- },
- {
- name: 'Visibility',
- value: displayData.visibility,
- inline: true
- },
- {
- name: 'Humidity',
- value: displayData.humidity,
- inline: true
- }
- )
- } else {
- embed.addFields(
- {
- name: 'General status',
- value: displayData.generalStatus,
- inline: true
- },
- {
- name: 'Temperature',
- value: displayData.temperature,
- inline: true
- },
- {
- name: "\u200B",
- value: '\u200B',
- inline: false
- },
- {
- name: 'Wind',
- value: displayData.wind,
- inline: true
- },
- {
- name: 'Cloud',
- value: displayData.cloud,
- inline: true
- },
- {
- name: 'Humidity',
- value: displayData.humidity,
- inline: true
- }
- )
- }
- interaction.reply({
- embeds: [
- embed
- ]
- })
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement