user command support

This commit is contained in:
derpystuff 2024-03-19 17:28:37 +01:00
parent af5a7f4252
commit 86936dc100
14 changed files with 710 additions and 11 deletions

View file

@ -0,0 +1,97 @@
const { createEmbed, formatPaginationEmbeds, page } = require('../../../../labscore/utils/embed')
const { link, iconPill, smallPill, icon, iconLinkPill, pill } = require('../../../../labscore/utils/markdown')
const { editOrReply } = require('../../../../labscore/utils/message')
const { paginator } = require('../../../../labscore/client');
const { dictionary } = require('../../../../labscore/api');
const { ApplicationCommandOptionTypes, InteractionCallbackTypes } = require('detritus-client/lib/constants');
const { TRANSLATE_LANGUAGE_MAPPINGS, DICTIONARY_LANGUAGES } = require('../../../../labscore/constants');
const LABELS = {
"offensive": `${iconPill("warning", "Offensive")}`
}
function createDictionaryPage(context, result, index, language){
let phon = ''
if(result.phonetic) phon = `\n*${result.phonetic}*`
let e = createEmbed("default", context, {
description: `${icon("definition")} **${link(`https://en.wiktionary.org/wiki/${encodeURIComponent(result.word)}`, result.word, "Definition on Wiktionary")}**`,
fields: []
})
if(result.phonetic) e.description += smallPill(result.phonetic)
if(language !== "en") e.description += `\n${TRANSLATE_LANGUAGE_MAPPINGS[language]} ${pill(DICTIONARY_LANGUAGES[language])}`
let word = result.entries[index]
let defItms = []
let i = 1;
for(const def of word.definitions.splice(0, 6)){
let entry = `${i}. ${def.definition}`
if(def.example) entry += `\n - *${def.example}*`
if(def.synonyms) entry += `\n${icon("empty")}${def.synonyms.splice(0, 4).map((s)=>smallPill(s)).join(' ')}`
defItms.push(entry)
i++
}
if(word.definitions.length >= 6 ) defItms.push(iconLinkPill("link", `https://www.google.com/search?q=define+${encodeURIComponent(result.word)}`, 'More results', 'View More Results'))
let type = word.type
if(word.labels) type += " " + word.labels.map((label)=>{if(LABELS[label]) return LABELS[label]; else return ""}).join(' ')
e.description += `\n\n**${type}**\n${defItms.join('\n\n')}`
return page(e);
}
module.exports = {
name: 'define',
description: 'Look up the definition of a word in the dictionary.',
contexts: [
0,
1,
2
],
integrationTypes: [
1
],
options: [
{
name: 'term',
description: 'Term to look up.',
type: ApplicationCommandOptionTypes.TEXT,
required: true
}
],
run: async (context, args) => {
await context.respond({data: {}, type: InteractionCallbackTypes.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE})
try{
let search = await dictionary(context, args.term, "en")
search = search.response
if(search.body.status == 1) return editOrReply(context, createEmbed("warning", context, search.body.message))
let pages = []
let i = 0;
for(const d of search.body.results[0].entries){
pages.push(createDictionaryPage(context, search.body.results[0], i, "en"))
i++;
}
await paginator.createPaginator({
context,
pages: formatPaginationEmbeds(pages)
});
}catch(e){
console.log(e)
if(e.response?.body?.status && e.response.body.status == 2) return editOrReply(context, createEmbed("warning", context, e.response.body.message))
return editOrReply(context, createEmbed("error", context, `Unable to perform dictionary lookup.`))
}
},
};

View file

@ -0,0 +1,61 @@
const { createEmbed } = require("../../../../labscore/utils/embed");
const { editOrReply } = require("../../../../labscore/utils/message");
const { webshot } = require("../../../../labscore/api/obelisk");
const { ApplicationCommandOptionTypes, InteractionCallbackTypes } = require('detritus-client/lib/constants');
module.exports = {
name: 'screenshot',
description: 'Create a screenshot of a website.',
contexts: [
0,
1,
2
],
integrationTypes: [
1
],
options: [
{
name: 'url',
description: 'Website URL.',
type: ApplicationCommandOptionTypes.TEXT,
required: true
}
],
run: async (context, args) => {
await context.respond({data: {}, type: InteractionCallbackTypes.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE})
await editOrReply(context, createEmbed("loading", context, `Creating website screenshot...`))
try {
const t = Date.now();
let ss = await webshot(context, args.url, context.channel.nsfw)
if (ss.response.body.status && ss.response.body.status !== 3) {
if (ss.response.body.image) return await editOrReply(context,
createEmbed("image", context, {
url: ss.response.body.image,
time: ((Date.now() - t) / 1000).toFixed(2)
})
)
return await editOrReply(context, createEmbed("error", context, "Unable to create screenshot."))
}
return await editOrReply(context, {
embeds: [createEmbed("image", context, {
url: "screenshot.png",
time: ((Date.now() - t) / 1000).toFixed(2)
})],
files: [{ filename: "screenshot.png", value: ss.response.body }]
})
} catch (e) {
console.log(e)
return await editOrReply(context, createEmbed("image", context, {
url: "https://bignutty.gitlab.io/webstorage4/v2/assets/screenshot/brand-update-2024/scr_unavailable.png"
}))
}
}
};

View file

@ -0,0 +1,76 @@
const { createEmbed } = require('../../../../labscore/utils/embed')
const { editOrReply } = require('../../../../labscore/utils/message')
const { darksky } = require('../../../../labscore/api');
const { pill, iconPill, smallPill, weatherIcon, timestamp } = require('../../../../labscore/utils/markdown');
const { ApplicationCommandOptionTypes, InteractionCallbackTypes } = require('detritus-client/lib/constants');
const { STATICS } = require('../../../../labscore/utils/statics');
module.exports = {
name: 'weather',
description: 'Check the weather at a location.',
contexts: [
0,
1,
2
],
integrationTypes: [
1
],
options: [
{
name: 'location',
description: 'City or place to check.',
type: ApplicationCommandOptionTypes.TEXT,
required: true
}
],
run: async (context, args) => {
await context.respond({data: {}, type: InteractionCallbackTypes.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE})
try{
let data = await darksky(context, args.location)
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 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 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){
console.log(e)
return editOrReply(context, createEmbed("warning", context, `No weather data available for given location.`))
}
},
};