discord_bot/commands/misc/weather.js
SileNce5k ecc162549e
Use replace instead of splitting for fahrenheit
Didn't know replace was a thing, so I did it in a kinda of shitty way
before. Shouldn't be much difference except for the code being more
understandable.
2023-12-04 16:01:57 +01:00

29 lines
No EOL
980 B
JavaScript

const getWeather = require('../../util/getWeather')
module.exports = {
name: "weather",
description: "Returns what the weather is for the specified location",
async execute({message, args}) {
if(args.length < 1){
message.channel.send(`You have to provide a location`)
return;
}
let location = args.join(" ")
let weather = {};
weather = await getWeather(location).catch((err) => {
console.log(err);
weather.weather = `An error occured while getting the weather for ${location}\nSee console for more info`;
weather.success = false;
});
// convert °C to °F and add it after C temperature in parentheses
let tempRegex = /(-?\d+)(?=°C)/g;
if(weather.success){
let tempInCelsius = weather.weather.match(tempRegex)[0];
let tempInFahrenheit = Math.round(tempInCelsius * 1.8 + 32);
weather.weather = weather.weather.replace("°C", `°C ${tempInFahrenheit}°F`)
}
message.channel.send(weather.weather);
}
}