The code imports various Discord-related modules and defines an array of command names (ALL_COMMANDS
) that will be registered or updated in the Discord bot. The syncTools
function retrieves a list of commands, updates the bot's commands, and assigns functions to each command in the interactionsCommands
object.
npm run import -- "discord handy tools"
const {
registerCommand, getCommands, deleteCommand, updateCommand
} = importer.import("discord api")
const {interactionsCommands} = importer.import("discord gateway")
const {doDiscordPdf} = importer.import("discord pdf converter")
const {
doInterpret, doSystemUsage, doSummary
} = importer.import("discord notebook connector")
const doMesh = importer.import("discord mesh generator")
const {doMention, doPrivate} = importer.import("discord llm interactions")
const ALL_COMMANDS = [
'pdf',
'interpret',
'usage',
'mesh',
'summary',
]
// bot commands using new API, same names as above but lower-case
async function syncTools(guildId = null) {
var cmd, cmdDef
var commandResult = await getCommands(guildId)
var commands = commandResult.map(command => command.name)
interactionsCommands['promptMention'] = doMention
interactionsCommands['promptPrivate'] = doPrivate
cmdDef = {
'name': 'pdf',
'description': 'Convert a website to PDF.',
'options': [
{
'name': 'address',
'description': 'Http address to convert.',
'required': true,
'type': 3
}
]
}
if(!commands.includes('pdf')) {
await registerCommand(cmdDef, null, guildId)
} else {
cmd = commandResult.filter(c => c.name == 'pdf')[0]
if(cmdDef.name != cmd.name || cmdDef.description != cmd.description)
await updateCommand(cmdDef, null, cmd.id, guildId)
}
interactionsCommands['pdf'] = doDiscordPdf
cmdDef = {
'name': 'interpret',
'description': 'Interpret what a tool will do.',
'options': [
{
'name': 'search',
'description': 'Search function for the tool name.',
'required': true,
'type': 3
}
]
}
if(!commands.includes('interpret')) {
await registerCommand(cmdDef, null, guildId)
} else {
cmd = commandResult.filter(c => c.name == 'interpret')[0]
if(cmdDef.name != cmd.name || cmdDef.description != cmd.description)
await updateCommand(cmdDef, null, cmd.id, guildId)
}
interactionsCommands['interpret'] = doInterpret
cmdDef = {
'name': 'usage',
'description': 'View system usage.',
'options': []
}
if(!commands.includes('usage')) {
await registerCommand(cmdDef, null, guildId)
} else {
cmd = commandResult.filter(c => c.name == 'usage')[0]
if(cmdDef.name != cmd.name || cmdDef.description != cmd.description)
await updateCommand(cmdDef, null, cmd.id, guildId)
}
interactionsCommands['usage'] = doSystemUsage
cmdDef = {
'name': 'summary',
'description': 'Summarize an article with LLM.',
'options': [
{
'name': 'url',
'description': 'Link address to summarize.',
'required': true,
'type': 3
}
]
}
if(!commands.includes('summary')) {
await registerCommand(cmdDef, null, guildId)
} else {
cmd = commandResult.filter(c => c.name == 'summary')[0]
if(cmdDef.name != cmd.name || cmdDef.description != cmd.description)
await updateCommand(cmdDef, null, cmd.id, guildId)
}
interactionsCommands['summary'] = doSummary
cmdDef = {
'name': 'mesh',
'description': 'LLaMa-Mesh gguf.',
'options': [
{
'name': 'text',
'description': 'Text to prompt mesh generator with.',
'required': true,
'type': 3
}
]
}
if(!commands.includes('mesh')) {
await registerCommand(cmdDef, null, guildId)
} else {
cmd = commandResult.filter(c => c.name == 'mesh')[0]
if(cmdDef.name != cmd.name || cmdDef.description != cmd.description)
await updateCommand(cmdDef, null, cmd.id, guildId)
}
interactionsCommands['mesh'] = doMesh
var toRemove = commandResult.filter(c => !ALL_COMMANDS.includes(c.name))
for(var i = 0; i < toRemove.length; i++) {
await deleteCommand(toRemove[i].id, guildId)
}
return await getCommands()
}
module.exports = syncTools
/**
* Synchronizes bot commands with the Discord API.
*
* @param {string} [guildId] - The ID of the guild to sync commands for.
* @returns {Promise<Command[]>} A promise resolving to an array of registered commands.
*/
const {
registerCommand,
getCommands,
deleteCommand,
updateCommand
} = require('discord-api').import();
const { interactionsCommands } = require('discord-gateway').import();
const { doDiscordPdf } = require('discord-pdf-converter').import();
const {
doInterpret,
doSystemUsage,
doSummary
} = require('discord-notebook-connector').import();
const doMesh = require('discord-mesh-generator').import();
const { doMention, doPrivate } = require('discord-llm-interactions').import();
const ALL_COMMANDS = [
'pdf',
'interpret',
'usage',
'mesh',
'summary',
];
/**
* Synchronizes bot commands with the Discord API.
*
* @param {string} [guildId] - The ID of the guild to sync commands for.
* @returns {Promise<Command[]>} A promise resolving to an array of registered commands.
*/
async function syncTools(guildId = null) {
const existingCommands = await getCommands(guildId);
const commands = existingCommands.map(command => command.name);
// Register or update command definitions
const commandDefs = [
{
name: 'pdf',
description: 'Convert a website to PDF.',
options: [
{
name: 'address',
description: 'Http address to convert.',
required: true,
type: 3
}
]
},
{
name: 'interpret',
description: 'Interpret what a tool will do.',
options: [
{
name:'search',
description: 'Search function for the tool name.',
required: true,
type: 3
}
]
},
{
name: 'usage',
description: 'View system usage.',
options: []
},
{
name:'summary',
description: 'Summarize an article with LLM.',
options: [
{
name: 'url',
description: 'Link address to summarize.',
required: true,
type: 3
}
]
},
{
name:'mesh',
description: 'LLaMa-Mesh gguf.',
options: [
{
name: 'text',
description: 'Text to prompt mesh generator with.',
required: true,
type: 3
}
]
}
];
for (const cmd of commandDefs) {
const existingCmd = existingCommands.find(c => c.name === cmd.name);
if (!existingCmd) {
// Register new command
await registerCommand(cmd, null, guildId);
} else if (cmd.name!== existingCmd.name || cmd.description!== existingCmd.description) {
// Update existing command
await updateCommand(cmd, null, existingCmd.id, guildId);
}
interactionsCommands[cmd.name] = getCommandFunction(cmd.name);
}
// Remove unused commands
const toRemove = existingCommands.filter(c =>!ALL_COMMANDS.includes(c.name));
for (const cmd of toRemove) {
await deleteCommand(cmd.id, guildId);
}
return await getCommands();
}
// Helper function to get command function from command name
function getCommandFunction(name) {
switch (name) {
case 'pdf':
return doDiscordPdf;
case 'interpret':
return doInterpret;
case 'usage':
return doSystemUsage;
case'summary':
return doSummary;
case'mesh':
return doMesh;
default:
throw new Error(`Unknown command: ${name}`);
}
}
module.exports = syncTools;
The code starts by importing various modules using the importer.import()
function. These modules are:
discord api
: provides registerCommand
, getCommands
, deleteCommand
, and updateCommand
functions.discord gateway
: provides the interactionsCommands
object.discord pdf converter
: provides the doDiscordPdf
function.discord notebook connector
: provides doInterpret
, doSystemUsage
, and doSummary
functions.discord mesh generator
: provides the doMesh
function.discord llm interactions
: provides doMention
and doPrivate
functions.The code defines an array of command names: ALL_COMMANDS
. This array contains the names of the commands that will be registered or updated in the Discord bot.
The syncTools
function is an asynchronous function that takes an optional guildId
parameter. It:
interactionsCommands
object with new functions.cmdDef
) for each command and checks if the command already exists.registerCommand
. Otherwise, it updates the command using updateCommand
if the command definition has changed.interactionsCommands
object.The code defines two command definitions (cmdDef
):
pdf
: converts a website to PDF.interpret
: interprets what a tool will do.