major cleanup

This commit is contained in:
derpystuff 2023-11-04 17:29:06 +01:00
parent ddd918470e
commit a8cf49e31e
60 changed files with 537 additions and 619 deletions

View file

@ -1,5 +1,5 @@
const { codeblock, highlight, icon, link, pill, smallPill, iconPill } = require('../../../labscore/utils/markdown')
const { createEmbed, formatPaginationEmbeds } = require('../../../labscore/utils/embed')
const { codeblock, icon, link, pill, smallPill, iconPill } = require('../../../labscore/utils/markdown')
const { createEmbed, formatPaginationEmbeds, page } = require('../../../labscore/utils/embed')
const { DISCORD_INVITES, DEFAULT_BOT_PREFIX } = require('../../../labscore/constants')
@ -9,86 +9,80 @@ const { editOrReply } = require('../../../labscore/utils/message');
const { Permissions } = require("detritus-client/lib/constants");
const { canUseLimitedTestCommands } = require('../../../labscore/utils/testing');
function createHelpPage(context, title, contents, descriptions){
return {
"embeds": [
createEmbed("default", context, {
description: `${title}\n\n` +
renderCommandList(contents, descriptions) +
`\n\n${icon("question")} Use ${smallPill(`${DEFAULT_BOT_PREFIX}help <command>`)} to view more information about a command.`
})
]
}
function createHelpPage(context, title, contents, descriptions) {
return page(createEmbed("default", context, {
description: `${title}\n\n` +
renderCommandList(contents, descriptions) +
`\n\n${icon("question")} Use ${smallPill(`${DEFAULT_BOT_PREFIX}help <command>`)} to view more information about a command.`
}))
}
function renderCommandList(commands, descriptions, limit){
function renderCommandList(commands, descriptions, limit) {
let len = Math.max(...(commands.map(el => el.length))) + 3;
let render = []
let i = 0;
for(const c of commands){
for (const c of commands) {
let pad = len - c.length;
let desc = descriptions[i]
if(desc.includes('\n')) desc = desc.split('\n')[0]
if(desc.length >= 41) desc = desc.substr(0, 40) + '...'
if (desc.includes('\n')) desc = desc.split('\n')[0]
if (desc.length >= 41) desc = desc.substr(0, 40) + '...'
render.push(` \` ${c}${' '.repeat(pad)}\` ${desc}`)
i++
}
if(limit && render.length > limit) render.splice(limit, 999)
if (limit && render.length > limit) render.splice(limit, 999)
return render.join('\n')
}
function createCommandPage(context, prefix, command){
function createCommandPage(context, prefix, command) {
alias = ' '
if(command.aliases.length >= 1){
for(const al of command.aliases) alias += smallPill(al)
if (command.aliases.length >= 1) {
for (const al of command.aliases) alias += smallPill(al)
alias += "\n"
}
let explicit = '';
if(command.metadata.explicit) explicit = `\n${icon('channel_nsfw')} This command contains explicit content and can only be used in Age-Restricted channels. ${link("https://support.discord.com/hc/en-us/articles/115000084051-Age-Restricted-Channels-and-Content", "Learn More")}\n`
if (command.metadata.explicit) explicit = `\n${icon('channel_nsfw')} This command contains explicit content and can only be used in Age-Restricted channels. ${link("https://support.discord.com/hc/en-us/articles/115000084051-Age-Restricted-Channels-and-Content", "Learn More")}\n`
// Render argument pills if present
let args = [];
if(command.argParser.args){
for(const a of command.argParser.args){
let argument = `-${a._name} <${a._type.replace('bool','true/false')}>`
if (command.argParser.args) {
for (const a of command.argParser.args) {
let argument = `-${a._name} <${a._type.replace('bool', 'true/false')}>`
argument = pill(argument)
if(a.help) argument += ` ${a.help}`
if (a.help) argument += ` ${a.help}`
argument += `\n `
if(a.default !== "") argument += ` ${smallPill(`default: ${a.default}`)}`
if(!a.required) argument += ` ${smallPill('optional')}`
if (a.default !== "") argument += ` ${smallPill(`default: ${a.default}`)}`
if (!a.required) argument += ` ${smallPill('optional')}`
args.push(argument)
}
}
let page = createEmbed("default", context, {
let cPage = createEmbed("default", context, {
description: `${icon("slash")} ${smallPill(command.name)}\n${alias}${explicit}\n${command.metadata.description}\n\n${args.join('\n\n')}`,
fields: []
})
// TODO: maybe try building a little parser that highlights things via ansi
if(command.metadata.usage) page.fields.push({
if (command.metadata.usage) cPage.fields.push({
name: `${icon("settings")} Usage`,
value: codeblock("py", [prefix + command.metadata.usage]),
inline: true
})
if(command.metadata.examples){
if (command.metadata.examples) {
let ex = []
for(const e of command.metadata.examples) ex.push(prefix + e)
page.fields.push({
for (const e of command.metadata.examples) ex.push(prefix + e)
cPage.fields.push({
name: `${icon("example")} Examples`,
value: '```' + ex.join('``````') + '```',
inline: false
})
}
return {
embeds: [page]
};
return page(cPage);
}
// These categories will be displayed to users, add them in the correct order
@ -115,64 +109,61 @@ module.exports = {
},
permissionsClient: [Permissions.EMBED_LINKS, Permissions.SEND_MESSAGES, Permissions.USE_EXTERNAL_EMOJIS, Permissions.READ_MESSAGE_HISTORY],
run: async (context, args) => {
if(canUseLimitedTestCommands(context)) categories["limited"] = `${icon("stars")} Limited Test Commands`;
else if(categories["limited"]) delete categories["limited"]
if (canUseLimitedTestCommands(context)) categories["limited"] = `${icon("stars")} Limited Test Commands`;
else if (categories["limited"]) delete categories["limited"]
if(args.command){
if (args.command) {
await context.triggerTyping()
// Detailed command view
let resultScores = {}
let resultMappings = {}
for(const c of context.commandClient.commands){
if(c.name.includes(args.command.toLowerCase()) || c.aliases.filter((f)=>{return f.includes(args.command.toLowerCase())}).length >= 1){
if(c.metadata.explicit && !context.channel.nsfw) continue;
if(!categories[c.metadata.category] && !context.user.isClientOwner) continue;
for (const c of context.commandClient.commands) {
if (c.name.includes(args.command.toLowerCase()) || c.aliases.filter((f) => { return f.includes(args.command.toLowerCase()) }).length >= 1) {
if (c.metadata.explicit && !context.channel.nsfw) continue;
if (!categories[c.metadata.category] && !context.user.isClientOwner) continue;
resultScores[c.name] = 1
resultMappings[c.name] = c
}
// Boost exact matches to rank higher in the result list
if(c.name == args.command.toLowerCase()) resultScores[c.name] += 1
if(c.aliases.filter((f)=>{return f == args.command.toLowerCase()}).length >= 1) resultScores[c.name] += 1
if (c.name == args.command.toLowerCase()) resultScores[c.name] += 1
if (c.aliases.filter((f) => { return f == args.command.toLowerCase() }).length >= 1) resultScores[c.name] += 1
}
let results = [];
resultScores = Object.fromEntries(Object.entries(resultScores).sort(([,a],[,b]) => b-a));
for(const k of Object.keys(resultScores)) results.push(resultMappings[k])
resultScores = Object.fromEntries(Object.entries(resultScores).sort(([, a], [, b]) => b - a));
for (const k of Object.keys(resultScores)) results.push(resultMappings[k])
let pages = []
let prefix = DEFAULT_BOT_PREFIX
try{
if(results.length == 0) return editOrReply(context, {embeds: [createEmbed("warning", context, "No commands found for the provided query.")]})
try {
if(results.length > 1) {
// Command overview
if (results.length == 0) return editOrReply(context, createEmbed("warning", context, "No commands found for the provided query."))
let cmds = results.map((m)=>{return m.name})
let dscs = results.map((m)=>{return m.metadata.description_short})
pages.push({embeds:[
createEmbed("default", context, {
if (results.length > 1) {
// Command overview
let cmds = results.map((m) => { return m.name })
let dscs = results.map((m) => { return m.metadata.description_short })
pages.push(page(createEmbed("default", context, {
description: `Check the pages for full command details.\n\n` + renderCommandList(cmds, dscs, 15) + `\n\n${icon("question")} Need help with something else? Contact us via our ${link(DISCORD_INVITES.support, "Support Server")}.`
})
]})
})))
// Generate command detail pages
for(const c of results){
pages.push(createCommandPage(context, prefix, c))
// Generate command detail pages
for (const c of results) {
pages.push(createCommandPage(context, prefix, c))
}
await paginator.createPaginator({
context,
pages: formatPaginationEmbeds(pages)
});
return;
} else {
return editOrReply(context, createCommandPage(context, prefix, results[0]))
}
pages = formatPaginationEmbeds(pages)
const paging = await paginator.createPaginator({
context,
pages
});
return;
} else {
return editOrReply(context, createCommandPage(context, prefix, results[0]))
}
}catch(e){
} catch (e) {
console.log(e)
}
} else {
@ -180,24 +171,23 @@ module.exports = {
let commands = {}
let descriptions = {}
for(const c of context.commandClient.commands){
if(!categories[c.metadata.category]) continue;
if(c.metadata.explicit && !context.channel.nsfw) continue;
if(!commands[c.metadata.category]) commands[c.metadata.category] = []
if(!descriptions[c.metadata.category]) descriptions[c.metadata.category] = []
for (const c of context.commandClient.commands) {
if (!categories[c.metadata.category]) continue;
if (c.metadata.explicit && !context.channel.nsfw) continue;
if (!commands[c.metadata.category]) commands[c.metadata.category] = []
if (!descriptions[c.metadata.category]) descriptions[c.metadata.category] = []
commands[c.metadata.category].push(`${c.name}`);
descriptions[c.metadata.category].push(`${c.metadata.description_short}`);
}
let pages = []
for(const cat of Object.keys(categories)){
for (const cat of Object.keys(categories)) {
pages.push(createHelpPage(context, categories[cat], commands[cat], descriptions[cat]))
}
pages = formatPaginationEmbeds(pages)
const paging = await paginator.createPaginator({
await paginator.createPaginator({
context,
pages
pages: formatPaginationEmbeds(pages)
});
}
},

View file

@ -18,10 +18,8 @@ module.exports = {
run: async (context) => {
context.triggerTyping();
ping = await context.client.ping()
editOrReply(context, {
embeds: [createEmbed("default", context, {
description: `${icon("latency")} **Pong!**\n` + codeblock("ansi", [`rest ${format(`${ping.rest}ms`, "m")}`, `gateway ${format(`${ping.gateway}ms`, "m")}`])
})]
})
editOrReply(context, createEmbed("default", context, {
description: `${icon("latency")} **Pong!**\n` + codeblock("ansi", [`rest ${format(`${ping.rest}ms`, "m")}`, `gateway ${format(`${ping.gateway}ms`, "m")}`])
}))
},
};

View file

@ -1,4 +1,4 @@
const { highlight, iconPill, link, icon, pill, linkPill, iconLinkPill } = require('../../../labscore/utils/markdown')
const { highlight, iconPill, iconLinkPill } = require('../../../labscore/utils/markdown')
const { createEmbed } = require('../../../labscore/utils/embed')
const { editOrReply } = require('../../../labscore/utils/message');
@ -78,7 +78,7 @@ module.exports = {
return;
}catch(e){
console.log(e)
return editOrReply(context, {embeds: [createEmbed("error", context, "Unable to fetch bot statistics.")]})
return editOrReply(context, createEmbed("error", context, "Unable to fetch bot statistics."))
}
}
};

View file

@ -19,7 +19,7 @@ module.exports = {
try{
if(typeof(args.amount) == "string") args.amount = parseInt(args.amount)
if(!args.amount) args.amount = 1
if(args.amount >= 6 || args.amount <= 0) return await editOrReply(context, {embeds:[createEmbed("warning", context, "Invalid Argument (amount)")]})
if(args.amount >= 6 || args.amount <= 0) return await editOrReply(context, createEmbed("warning", context, "Invalid Argument (amount)"))
let cmds = [];
let found = 0;
for(const c of context.commandClient.replies.toArray().reverse()){