mirror of
https://gitlab.com/bignutty/labscore.git
synced 2025-06-08 06:03:04 -04:00
add fahrenheit
This commit is contained in:
parent
d83a4d0503
commit
592f75762f
2 changed files with 182 additions and 85 deletions
|
@ -1,6 +1,7 @@
|
||||||
const { darksky } = require('#api');
|
const { darksky } = require('#api');
|
||||||
|
const { paginator } = require('#client');
|
||||||
|
|
||||||
const { createEmbed } = require('#utils/embed');
|
const { createEmbed, page } = require('#utils/embed');
|
||||||
const { acknowledge } = require('#utils/interactions');
|
const { acknowledge } = require('#utils/interactions');
|
||||||
const { pill, iconPill, smallPill, weatherIcon, timestamp, icon, link, stringwrap} = require('#utils/markdown');
|
const { pill, iconPill, smallPill, weatherIcon, timestamp, icon, link, stringwrap} = require('#utils/markdown');
|
||||||
const { editOrReply } = require('#utils/message');
|
const { editOrReply } = require('#utils/message');
|
||||||
|
@ -8,6 +9,69 @@ const { STATICS } = require('#utils/statics');
|
||||||
|
|
||||||
const { ApplicationCommandOptionTypes } = require('detritus-client/lib/constants');
|
const { ApplicationCommandOptionTypes } = require('detritus-client/lib/constants');
|
||||||
|
|
||||||
|
const modifiers = {
|
||||||
|
"°C": (i)=>i,
|
||||||
|
"°F": (i)=>(i*(9/5))+32
|
||||||
|
}
|
||||||
|
|
||||||
|
const unitNames = {
|
||||||
|
"°C": "Celcius",
|
||||||
|
"°F": "Fahrenheit"
|
||||||
|
}
|
||||||
|
|
||||||
|
function temperature(value, units){
|
||||||
|
return `${Math.floor(modifiers[units](value))}${units}`
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderWeatherCard(context, data, units){
|
||||||
|
let description = `### ${weatherIcon(data.result.current.icon.id)} ${temperature(data.result.current.temperature.current, units)} • ${data.result.current.condition.label}\n\n${pill("Feels like")} ${smallPill(temperature(data.result.current.temperature.feels_like, units))} ${pill("Wind")} ${smallPill(data.result.current.wind.speed + " km/h")}`
|
||||||
|
|
||||||
|
let secondaryPills = [];
|
||||||
|
if(data.result.current.humidity > 0) secondaryPills.push(`${pill("Humidity")} ${smallPill(Math.floor(data.result.current.humidity * 100) + "%")}`)
|
||||||
|
if(data.result.current.uvindex > 0) secondaryPills.push(`${pill("UV Index")} ${smallPill(data.result.current.uvindex)}`)
|
||||||
|
|
||||||
|
if(secondaryPills.length >= 1) description += '\n' + secondaryPills.join(` `)
|
||||||
|
|
||||||
|
description += `\n\n${iconPill("sun", "Sunrise")} ${timestamp(data.result.current.sun.sunrise, "t")} ${iconPill("moon", "Sunset")} ${timestamp(data.result.current.sun.sunset, "t")}`
|
||||||
|
|
||||||
|
// Render weather alerts
|
||||||
|
if(data.result.warnings.length >= 1){
|
||||||
|
for(const w of data.result.warnings.splice(0, 1)){
|
||||||
|
if(description.includes(stringwrap(w.label, 50))) continue;
|
||||||
|
description += `\n\n${icon("warning")} **${stringwrap(w.label, 50)}**\n-# ${stringwrap(w.source, 50)} • ${link(w.url, "Learn More", "Learn more about this alert")}`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Render Forecasts
|
||||||
|
description += `\n`
|
||||||
|
|
||||||
|
let space = 3;
|
||||||
|
if(units === "°F") space = 4;
|
||||||
|
for(const i of data.result.forecast){
|
||||||
|
description += `\n${pill(i.day)} ${weatherIcon(i.icon)}`
|
||||||
|
if(temperature(i.temperature.max, units).toString().length === space) description += `${pill(temperature(i.temperature.max, units) + " ")}`
|
||||||
|
else description += `${pill(temperature(i.temperature.max, units))}`
|
||||||
|
description += `**/**`
|
||||||
|
if(temperature(i.temperature.min, units).toString().length === space) description += `${smallPill(temperature(i.temperature.min, units) + " ")}`
|
||||||
|
else description += `${smallPill(temperature(i.temperature.min, units))}`
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
let e = createEmbed("default", context, {
|
||||||
|
description,
|
||||||
|
timestamp: new Date(data.result.current.date)
|
||||||
|
})
|
||||||
|
|
||||||
|
e.footer.iconUrl = STATICS.weather
|
||||||
|
if(data.result.location) e.footer.text = data.result.location
|
||||||
|
|
||||||
|
if(data.result.current.icon) e.thumbnail = { url: data.result.current.icon.url }
|
||||||
|
if(data.result.current.image) e.image = { url: data.result.current.image }
|
||||||
|
|
||||||
|
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name: 'weather',
|
name: 'weather',
|
||||||
description: 'Check the weather at a location.',
|
description: 'Check the weather at a location.',
|
||||||
|
@ -26,6 +90,22 @@ module.exports = {
|
||||||
type: ApplicationCommandOptionTypes.TEXT,
|
type: ApplicationCommandOptionTypes.TEXT,
|
||||||
required: true
|
required: true
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: 'units',
|
||||||
|
description: 'Temperature units to use.',
|
||||||
|
type: ApplicationCommandOptionTypes.TEXT,
|
||||||
|
choices: [
|
||||||
|
{
|
||||||
|
value: "celcius",
|
||||||
|
name: "Celcius"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: "fahrenheit",
|
||||||
|
name: "Fahrenheit"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
required: false
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: 'incognito',
|
name: 'incognito',
|
||||||
description: 'Makes the response only visible to you.',
|
description: 'Makes the response only visible to you.',
|
||||||
|
@ -42,48 +122,23 @@ module.exports = {
|
||||||
|
|
||||||
data = data.response.body
|
data = data.response.body
|
||||||
|
|
||||||
let description = `### ${weatherIcon(data.result.current.icon.id)} ${Math.floor(data.result.current.temperature.current)}°C • ${data.result.current.condition.label}\n\n${pill("Feels like")} ${smallPill(Math.floor(data.result.current.temperature.feels_like) + "°C")} ${pill("Wind")} ${smallPill(data.result.current.wind.speed + " km/h")}`
|
let units = ["°C", "°F"]
|
||||||
|
if(["f","fahrenheit","°f"].includes(args.units.toLowerCase())) units = ["°F", "°C"]
|
||||||
|
|
||||||
let secondaryPills = [];
|
let pages = []
|
||||||
if(data.result.current.humidity > 0) secondaryPills.push(`${pill("Humidity")} ${smallPill(Math.floor(data.result.current.humidity * 100) + "%")}`)
|
pages.push(page(renderWeatherCard(context, data, units[0])))
|
||||||
if(data.result.current.uvindex > 0) secondaryPills.push(`${pill("UV Index")} ${smallPill(data.result.current.uvindex)}`)
|
pages.push(page(renderWeatherCard(context, data, units[1])))
|
||||||
|
|
||||||
if(secondaryPills.length >= 1) description += '\n' + secondaryPills.join(` `)
|
await paginator.createPaginator({
|
||||||
|
context,
|
||||||
description += `\n\n${iconPill("sun", "Sunrise")} ${timestamp(data.result.current.sun.sunrise, "t")} ${iconPill("moon", "Sunset")} ${timestamp(data.result.current.sun.sunset, "t")}`
|
pages,
|
||||||
|
buttons: [{
|
||||||
// Render weather alerts
|
customId: "next",
|
||||||
if(data.result.warnings.length >= 1){
|
emoji: "<:ico_button_thermometer:1262512806633144382>",
|
||||||
for(const w of data.result.warnings.splice(0, 1)){
|
label: `Toggle ${unitNames[units[0]]}/${unitNames[units[1]]}`,
|
||||||
if(description.includes(stringwrap(w.label, 50))) continue;
|
style: 2
|
||||||
description += `\n\n${icon("warning")} **${stringwrap(w.label, 50)}**\n-# ${stringwrap(w.source, 50)} • ${link(w.url, "Learn More", "Learn more about this alert")}`
|
}]
|
||||||
}
|
});
|
||||||
}
|
|
||||||
|
|
||||||
// Render Forecasts
|
|
||||||
description += `\n`
|
|
||||||
for(const i of data.result.forecast){
|
|
||||||
description += `\n${pill(i.day)} ${weatherIcon(i.icon)}`
|
|
||||||
if(Math.floor(i.temperature.max).toString().length === 1) description += `${pill(Math.floor(i.temperature.max) + "°C ")}`
|
|
||||||
else description += `${pill(Math.floor(i.temperature.max) + "°C")}`
|
|
||||||
description += `**/**`
|
|
||||||
if(Math.floor(i.temperature.min).toString().length === 1) description += `${smallPill(Math.floor(i.temperature.min) + "°C ")}`
|
|
||||||
else description += `${smallPill(Math.floor(i.temperature.min) + "°C")}`
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
let e = createEmbed("default", context, {
|
|
||||||
description,
|
|
||||||
timestamp: new Date(data.result.current.date)
|
|
||||||
})
|
|
||||||
|
|
||||||
e.footer.iconUrl = STATICS.weather
|
|
||||||
if(data.result.location) e.footer.text = data.result.location //+ " • " + context.client.user.username
|
|
||||||
|
|
||||||
if(data.result.current.icon) e.thumbnail = { url: data.result.current.icon.url }
|
|
||||||
if(data.result.current.image) e.image = { url: data.result.current.image }
|
|
||||||
|
|
||||||
return editOrReply(context, e)
|
|
||||||
}catch(e){
|
}catch(e){
|
||||||
console.log(e)
|
console.log(e)
|
||||||
return editOrReply(context, createEmbed("warning", context, `No weather data available for given location.`))
|
return editOrReply(context, createEmbed("warning", context, `No weather data available for given location.`))
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
const { darksky } = require('#api');
|
const { darksky } = require('#api');
|
||||||
|
const { paginator } = require('#client');
|
||||||
|
|
||||||
const { createEmbed } = require('#utils/embed')
|
const { createEmbed, page } = require('#utils/embed')
|
||||||
const { pill, iconPill, smallPill, weatherIcon, timestamp, icon, link} = require('#utils/markdown');
|
const { pill, iconPill, smallPill, weatherIcon, timestamp, icon, link} = require('#utils/markdown');
|
||||||
const { editOrReply } = require('#utils/message')
|
const { editOrReply } = require('#utils/message')
|
||||||
const { STATICS } = require('#utils/statics');
|
const { STATICS } = require('#utils/statics');
|
||||||
|
@ -8,28 +9,22 @@ const { STATICS } = require('#utils/statics');
|
||||||
// TODO: Turn this into a general purpose permissions constant
|
// TODO: Turn this into a general purpose permissions constant
|
||||||
const { Permissions } = require("detritus-client/lib/constants");
|
const { Permissions } = require("detritus-client/lib/constants");
|
||||||
|
|
||||||
module.exports = {
|
const modifiers = {
|
||||||
name: 'weather',
|
"°C": (i)=>i,
|
||||||
aliases: ['forecast'],
|
"°F": (i)=>(i*(9/5))+32
|
||||||
label: 'query',
|
}
|
||||||
metadata: {
|
|
||||||
description: 'Displays information about the weather.',
|
|
||||||
description_short: 'Local weather information',
|
|
||||||
examples: ['weather Otter, Germany'],
|
|
||||||
category: 'utils',
|
|
||||||
usage: 'weather <location>',
|
|
||||||
slashCommand: "weather"
|
|
||||||
},
|
|
||||||
permissionsClient: [Permissions.EMBED_LINKS, Permissions.SEND_MESSAGES, Permissions.USE_EXTERNAL_EMOJIS, Permissions.READ_MESSAGE_HISTORY],
|
|
||||||
run: async (context, args) => {
|
|
||||||
context.triggerTyping();
|
|
||||||
if(!args.query) return editOrReply(context, createEmbed("warning", context, `Missing Parameter (location).`))
|
|
||||||
try{
|
|
||||||
let data = await darksky(context, args.query)
|
|
||||||
|
|
||||||
data = data.response.body
|
const unitNames = {
|
||||||
|
"°C": "Celcius",
|
||||||
|
"°F": "Fahrenheit"
|
||||||
|
}
|
||||||
|
|
||||||
let description = `### ${weatherIcon(data.result.current.icon.id)} ${Math.floor(data.result.current.temperature.current)}°C • ${data.result.current.condition.label}\n\n${pill("Feels like")} ${smallPill(Math.floor(data.result.current.temperature.feels_like) + "°C")} ${pill("Wind")} ${smallPill(data.result.current.wind.speed + " km/h")}`
|
function temperature(value, units){
|
||||||
|
return `${Math.floor(modifiers[units](value))}${units}`
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderWeatherCard(context, data, units){
|
||||||
|
let description = `### ${weatherIcon(data.result.current.icon.id)} ${temperature(data.result.current.temperature.current, units)} • ${data.result.current.condition.label}\n\n${pill("Feels like")} ${smallPill(temperature(data.result.current.temperature.feels_like, units))} ${pill("Wind")} ${smallPill(data.result.current.wind.speed + " km/h")}`
|
||||||
|
|
||||||
let secondaryPills = [];
|
let secondaryPills = [];
|
||||||
if(data.result.current.humidity > 0) secondaryPills.push(`${pill("Humidity")} ${smallPill(Math.floor(data.result.current.humidity * 100) + "%")}`)
|
if(data.result.current.humidity > 0) secondaryPills.push(`${pill("Humidity")} ${smallPill(Math.floor(data.result.current.humidity * 100) + "%")}`)
|
||||||
|
@ -49,13 +44,16 @@ module.exports = {
|
||||||
|
|
||||||
// Render Forecasts
|
// Render Forecasts
|
||||||
description += `\n`
|
description += `\n`
|
||||||
|
|
||||||
|
let space = 3;
|
||||||
|
if(units === "°F") space = 4;
|
||||||
for(const i of data.result.forecast){
|
for(const i of data.result.forecast){
|
||||||
description += `\n${pill(i.day)} ${weatherIcon(i.icon)}`
|
description += `\n${pill(i.day)} ${weatherIcon(i.icon)}`
|
||||||
if(Math.floor(i.temperature.max).toString().length === 1) description += `${pill(Math.floor(i.temperature.max) + "°C ")}`
|
if(temperature(i.temperature.max, units).toString().length === space) description += `${pill(temperature(i.temperature.max, units) + " ")}`
|
||||||
else description += `${pill(Math.floor(i.temperature.max) + "°C")}`
|
else description += `${pill(temperature(i.temperature.max, units))}`
|
||||||
description += `**/**`
|
description += `**/**`
|
||||||
if(Math.floor(i.temperature.min).toString().length === 1) description += `${smallPill(Math.floor(i.temperature.min) + "°C ")}`
|
if(temperature(i.temperature.min, units).toString().length === space) description += `${smallPill(temperature(i.temperature.min, units) + " ")}`
|
||||||
else description += `${smallPill(Math.floor(i.temperature.min) + "°C")}`
|
else description += `${smallPill(temperature(i.temperature.min, units))}`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -65,14 +63,58 @@ module.exports = {
|
||||||
})
|
})
|
||||||
|
|
||||||
e.footer.iconUrl = STATICS.weather
|
e.footer.iconUrl = STATICS.weather
|
||||||
if(data.result.location) e.footer.text = data.result.location //+ " • " + context.client.user.username
|
if(data.result.location) e.footer.text = data.result.location
|
||||||
|
|
||||||
if(data.result.current.icon) e.thumbnail = { url: data.result.current.icon.url }
|
if(data.result.current.icon) e.thumbnail = { url: data.result.current.icon.url }
|
||||||
if(data.result.current.image) e.image = { url: data.result.current.image }
|
if(data.result.current.image) e.image = { url: data.result.current.image }
|
||||||
|
|
||||||
return editOrReply(context, e)
|
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
name: 'weather',
|
||||||
|
aliases: ['forecast'],
|
||||||
|
label: 'query',
|
||||||
|
metadata: {
|
||||||
|
description: 'Displays information about the weather.',
|
||||||
|
description_short: 'Local weather information',
|
||||||
|
examples: ['weather Otter, Germany -t f'],
|
||||||
|
category: 'utils',
|
||||||
|
usage: 'weather <location> [-t <c|f>',
|
||||||
|
slashCommand: "weather"
|
||||||
|
},
|
||||||
|
args: [
|
||||||
|
{name: 't', default: 'celcius', type: 'units', help: "Temperature Units to use."},
|
||||||
|
],
|
||||||
|
permissionsClient: [Permissions.EMBED_LINKS, Permissions.SEND_MESSAGES, Permissions.USE_EXTERNAL_EMOJIS, Permissions.READ_MESSAGE_HISTORY],
|
||||||
|
run: async (context, args) => {
|
||||||
|
context.triggerTyping();
|
||||||
|
if(!args.query) return editOrReply(context, createEmbed("warning", context, `Missing Parameter (location).`))
|
||||||
|
try{
|
||||||
|
let data = await darksky(context, args.query)
|
||||||
|
|
||||||
|
data = data.response.body
|
||||||
|
|
||||||
|
let units = ["°C", "°F"]
|
||||||
|
if(["f","fahrenheit","°f"].includes(args.t.toLowerCase())) units = ["°F", "°C"]
|
||||||
|
|
||||||
|
let pages = []
|
||||||
|
pages.push(page(renderWeatherCard(context, data, units[0])))
|
||||||
|
pages.push(page(renderWeatherCard(context, data, units[1])))
|
||||||
|
|
||||||
|
await paginator.createPaginator({
|
||||||
|
context,
|
||||||
|
pages,
|
||||||
|
buttons: [{
|
||||||
|
customId: "next",
|
||||||
|
emoji: "<:ico_button_thermometer:1262512806633144382>",
|
||||||
|
label: `Toggle ${unitNames[units[0]]}/${unitNames[units[1]]}`,
|
||||||
|
style: 2
|
||||||
|
}]
|
||||||
|
});
|
||||||
}catch(e){
|
}catch(e){
|
||||||
return editOrReply(context, createEmbed("warning", context, `No weather data available for given location.`))
|
return editOrReply(context, createEmbed("warning", context, `No weather data available for given location.`))
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
};
|
};
|
Loading…
Add table
Add a link
Reference in a new issue