The code imports various modules to interact with the Discord API and language models, and defines a list of commands to be registered with the Discord API. The syncChat
function synchronizes the chat commands with the Discord API, registering the hello
and prompt
commands if they are not already present, and mapping them to corresponding functions from the discord llm interactions
module.
npm run import -- "sync discord llm tools"
const {
registerCommand, getCommands, deleteCommand, updateCommand
} = importer.import("discord api")
const {interactionsCommands} = importer.import("discord gateway")
const {
doInteraction, doMention, doPrivate
} = importer.import("discord llm interactions")
const {
doDistill, doInstruct, doReason, doMistral
} = importer.import("discord llm connector")
const {
doEssay, doBusiness, doStory
} = importer.import("discord writing llms")
const ALL_COMMANDS = [
'hello',
'prompt',
'distill',
'instruct',
'reason',
//'voice',
'essay',
'business',
'story',
'mistral',
]
// bot commands using new API, same names as above but lower-case
async function syncChat(guildId = null) {
var cmd, cmdDef
var commandResult = await getCommands(guildId)
var commands = commandResult.map(command => command.name)
if(!commands.includes('hello')) {
await registerCommand('hello', 'Check if Orbb is awake.', guildId)
} else {
cmd = commandResult.filter(c => c.name == 'hello')[0]
//await updateCommand(cmd.name, cmd.description, cmd.id, guildId)
}
interactionsCommands['hello'] = doInteraction
cmdDef = {
'name': 'prompt',
'description': 'Prompt an llm.',
'options': [
{
'name': 'text',
'description': 'Text to prompt the llm with.',
'required': true,
'type': 3
}
]
}
if(!commands.includes('prompt')) {
await registerCommand(cmdDef, null, guildId)
} else {
cmd = commandResult.filter(c => c.name == 'prompt')[0]
if(cmdDef.name != cmd.name || cmdDef.description != cmd.description
|| cmdDef.options.length != cmd.options.length
|| JSON.stringify(cmdDef.options) != JSON.stringify(cmd.options.map(o => ({
name: o.name,
description: o.description,
required: o.required,
type: o.type
})))) {
debugger
await updateCommand(cmdDef, null, cmd.id, guildId)
}
}
interactionsCommands['prompt'] = doInteraction
interactionsCommands['promptMention'] = doMention
interactionsCommands['promptPrivate'] = doPrivate
cmdDef = {
'name': 'distill',
'description': 'Distill with DeepSeek R1 8B.',
'options': [
{
'name': 'text',
'description': 'Text to prompt the DeepSeek R1 8B with.',
'required': true,
'type': 3
}
]
}
if(!commands.includes('distill')) {
await registerCommand(cmdDef, null, guildId)
} else {
cmd = commandResult.filter(c => c.name == 'distill')[0]
if(cmdDef.name != cmd.name || cmdDef.description != cmd.description)
await updateCommand(cmdDef, null, cmd.id, guildId)
}
interactionsCommands['distill'] = doDistill
cmdDef = {
'name': 'instruct',
'description': 'Meta Instruct 70B.',
'options': [
{
'name': 'text',
'description': 'Text to prompt the Meta Instruct 70B with.',
'required': true,
'type': 3
}
]
}
if(!commands.includes('instruct')) {
await registerCommand(cmdDef, null, guildId)
} else {
cmd = commandResult.filter(c => c.name == 'instruct')[0]
if(cmdDef.name != cmd.name || cmdDef.description != cmd.description)
await updateCommand(cmdDef, null, cmd.id, guildId)
}
interactionsCommands['instruct'] = doInstruct
cmdDef = {
'name': 'mistral',
'description': 'Meta Instruct 70B.',
'options': [
{
'name': 'text',
'description': 'Text to prompt the Meta Instruct 70B with.',
'required': true,
'type': 3
}
]
}
if(!commands.includes('mistral')) {
await registerCommand(cmdDef, null, guildId)
} else {
cmd = commandResult.filter(c => c.name == 'mistral')[0]
if(cmdDef.name != cmd.name || cmdDef.description != cmd.description)
await updateCommand(cmdDef, null, cmd.id, guildId)
}
interactionsCommands['mistral'] = doMistral
cmdDef = {
'name': 'reason',
'description': 'Qwen 2.5 14B M1.',
'options': [
{
'name': 'text',
'description': 'Text to prompt Qwen reasoning LLM.',
'required': true,
'type': 3
}
]
}
if(!commands.includes('reason')) {
await registerCommand(cmdDef, null, guildId)
} else {
cmd = commandResult.filter(c => c.name == 'reason')[0]
if(cmdDef.name != cmd.name || cmdDef.description != cmd.description)
await updateCommand(cmdDef, null, cmd.id, guildId)
}
interactionsCommands['reason'] = doReason
cmdDef = {
'name': 'essay',
'description': 'Write an essay.',
'options': [
{
'name': 'text',
'description': 'Text to prompt LLM.',
'required': true,
'type': 3
},
{
'name': 'model',
'description': 'Model to use, Meta, DeepSeek, Llama, or Qwen.',
'required': false,
'type': 3,
'choices': [
{name: 'Meta', value: 'Meta'},
{name: 'DeepSeek', value: 'DeepSeek'},
{name: 'Llama', value: 'Llama'},
{name: 'Qwen', value: 'Qwen'},
{name: 'Code', value: 'Code'},
{name: 'Mistral', value: 'Mistral'},
]
}
]
}
if(!commands.includes('essay')) {
await registerCommand(cmdDef, null, guildId)
} else {
cmd = commandResult.filter(c => c.name == 'essay')[0]
if(cmdDef.name != cmd.name || cmdDef.description != cmd.description)
await updateCommand(cmdDef, null, cmd.id, guildId)
}
interactionsCommands['essay'] = doEssay
cmdDef = {
'name': 'business',
'description': 'Write an business plan.',
'options': [
{
'name': 'text',
'description': 'Text to prompt LLM.',
'required': true,
'type': 3
},
{
'name': 'model',
'description': 'Model to use, Meta, DeepSeek, Llama, or Qwen.',
'required': false,
'type': 3,
'choices': [
{name: 'Meta', value: 'Meta'},
{name: 'DeepSeek', value: 'DeepSeek'},
{name: 'Llama', value: 'Llama'},
{name: 'Qwen', value: 'Qwen'},
{name: 'Code', value: 'Code'},
{name: 'Mistral', value: 'Mistral'},
]
}
]
}
if(!commands.includes('business')) {
await registerCommand(cmdDef, null, guildId)
} else {
cmd = commandResult.filter(c => c.name == 'business')[0]
if(cmdDef.name != cmd.name || cmdDef.description != cmd.description)
await updateCommand(cmdDef, null, cmd.id, guildId)
}
interactionsCommands['business'] = doBusiness
cmdDef = {
'name': 'story',
'description': 'Write an story.',
'options': [
{
'name': 'text',
'description': 'Text to prompt LLM.',
'required': true,
'type': 3
},
{
'name': 'model',
'description': 'Model to use, Meta, DeepSeek, Llama, or Qwen.',
'required': false,
'type': 3,
'choices': [
{name: 'Meta', value: 'Meta'},
{name: 'DeepSeek', value: 'DeepSeek'},
{name: 'Llama', value: 'Llama'},
{name: 'Qwen', value: 'Qwen'},
{name: 'Code', value: 'Code'},
{name: 'Mistral', value: 'Mistral'},
]
}
]
}
if(!commands.includes('story')) {
await registerCommand(cmdDef, null, guildId)
} else {
cmd = commandResult.filter(c => c.name == 'story')[0]
if(cmdDef.name != cmd.name || cmdDef.description != cmd.description)
await updateCommand(cmdDef, null, cmd.id, guildId)
}
interactionsCommands['story'] = doStory
/*
cmdDef = {
'name': 'voice',
'description': 'OuteTTS 0.3 1B/Llasa 3B gguf.',
'options': [
{
'name': 'text',
'description': 'Text to convert to audio.',
'required': true,
'type': 3
}
]
}
if(!commands.includes('voice')) {
await registerCommand(cmdDef, null, guildId)
} else {
cmd = commandResult.filter(c => c.name == 'voice')[0]
if(cmdDef.name != cmd.name || cmdDef.description != cmd.description)
await updateCommand(cmdDef, null, cmd.id, guildId)
}
interactionsCommands['voice'] = doVoice
*/
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 = syncChat
// Import required modules
const {
registerCommand, getCommands, deleteCommand, updateCommand
} = require('discord-api');
const { interactionsCommands } = require('discord-gateway');
const {
doInteraction, doMention, doPrivate
} = require('discord-llm-interactions');
const {
doDistill, doInstruct, doReason, doMistral
} = require('discord-llm-connector');
const {
doEssay, doBusiness, doStory
} = require('discord-writing-llms');
// Define commands with their default options
const commands = {
hello: {
name: 'hello',
description: 'Check if Orbb is awake.',
},
prompt: {
name: 'prompt',
description: 'Prompt an llm.',
options: [
{
name: 'text',
description: 'Text to prompt the llm with.',
required: true,
type: 3
}
]
},
distill: {
name: 'distill',
description: 'Distill with DeepSeek R1 8B.',
options: [
{
name: 'text',
description: 'Text to prompt the DeepSeek R1 8B with.',
required: true,
type: 3
}
]
},
instruct: {
name: 'instruct',
description: 'Meta Instruct 70B.',
options: [
{
name: 'text',
description: 'Text to prompt the Meta Instruct 70B with.',
required: true,
type: 3
}
]
},
mistral: {
name:'mistral',
description: 'Meta Instruct 70B.',
options: [
{
name: 'text',
description: 'Text to prompt the Meta Instruct 70B with.',
required: true,
type: 3
}
]
},
reason: {
name:'reason',
description: 'Qwen 2.5 14B M1.',
options: [
{
name: 'text',
description: 'Text to prompt Qwen reasoning LLM.',
required: true,
type: 3
}
]
},
essay: {
name: 'essay',
description: 'Write an essay.',
options: [
{
name: 'text',
description: 'Text to prompt LLM.',
required: true,
type: 3
},
{
name:'model',
description: 'Model to use, Meta, DeepSeek, Llama, or Qwen.',
required: false,
type: 3,
choices: [
{ name: 'Meta', value: 'Meta' },
{ name: 'DeepSeek', value: 'DeepSeek' },
{ name: 'Llama', value: 'Llama' },
{ name: 'Qwen', value: 'Qwen' },
{ name: 'Code', value: 'Code' },
{ name: 'Mistral', value: 'Mistral' },
]
}
]
},
business: {
name: 'business',
description: 'Write an business plan.',
options: [
{
name: 'text',
description: 'Text to prompt LLM.',
required: true,
type: 3
},
{
name:'model',
description: 'Model to use, Meta, DeepSeek, Llama, or Qwen.',
required: false,
type: 3,
choices: [
{ name: 'Meta', value: 'Meta' },
{ name: 'DeepSeek', value: 'DeepSeek' },
{ name: 'Llama', value: 'Llama' },
{ name: 'Qwen', value: 'Qwen' },
{ name: 'Code', value: 'Code' },
{ name: 'Mistral', value: 'Mistral' },
]
}
]
},
story: {
name:'story',
description: 'Write an story.',
options: [
{
name: 'text',
description: 'Text to prompt LLM.',
required: true,
type: 3
},
{
name:'model',
description: 'Model to use, Meta, DeepSeek, Llama, or Qwen.',
required: false,
type: 3,
choices: [
{ name: 'Meta', value: 'Meta' },
{ name: 'DeepSeek', value: 'DeepSeek' },
{ name: 'Llama', value: 'Llama' },
{ name: 'Qwen', value: 'Qwen' },
{ name: 'Code', value: 'Code' },
{ name: 'Mistral', value: 'Mistral' },
]
}
]
},
};
// Define all available commands
const allCommands = [
'hello',
'prompt',
'distill',
'instruct',
'mistral',
'reason',
'essay',
'business',
'story',
];
// Function to sync chat commands
async function syncChat(guildId = null) {
try {
// Get all commands in the guild
const commandsResult = await getCommands(guildId);
// Filter out commands that are not in our list
const commandsToCreate = commandsResult.filter((command) =>!allCommands.includes(command.name));
// Create commands that are not present
for (const command of commandsToCreate) {
await registerCommand(command, null, guildId);
}
// Map commands to their corresponding functions
const interactions = {
hello: doInteraction,
prompt: doInteraction,
promptMention: doMention,
promptPrivate: doPrivate,
distill: doDistill,
instruct: doInstruct,
mistral: doMistral,
reason: doReason,
essay: doEssay,
business: doBusiness,
story: doStory,
};
// Update commands that have changed
for (const command of allCommands) {
const commandDef = commands[command];
const commandResult = commandsResult.find((c) => c.name === command);
if (commandResult && commandDef.name!== commandResult.name || commandDef.description!== commandResult.description) {
await updateCommand(commandDef, null, commandResult.id, guildId);
// Update interactions mapping
if (interactions[command]) {
delete interactions[command];
interactions[command] = doInteraction;
}
} else if (!commandResult) {
// Create command
await registerCommand(commandDef, null, guildId);
// Update interactions mapping
interactions[command] = doInteraction;
}
}
// Delete commands that are not present in our list
const commandsToDelete = commandsResult.filter((command) =>!allCommands.includes(command.name));
for (const command of commandsToDelete) {
await deleteCommand(command.id, guildId);
}
// Return all commands
return await getCommands();
} catch (error) {
console.error('Error syncing chat commands:', error);
throw error;
}
}
module.exports = syncChat;
The code imports several modules using the importer.import
function:
discord api
: This module provides functions to interact with the Discord API, including registerCommand
, getCommands
, deleteCommand
, and updateCommand
.discord gateway
: This module provides an interactionsCommands
object that maps commands to functions.discord llm interactions
: This module provides functions to interact with the language model, including doInteraction
, doMention
, and doPrivate
.discord llm connector
: This module provides functions to interact with the language model connector, including doDistill
, doInstruct
, doReason
, and doMistral
.discord writing llms
: This module provides functions to interact with the writing language models, including doEssay
, doBusiness
, and doStory
.The code defines a list of commands ALL_COMMANDS
that includes the names of the commands that will be registered with the Discord API.
The syncChat
function is an asynchronous function that synchronizes the chat commands with the Discord API. It takes an optional guildId
parameter.
getCommands
.hello
command is present in the list. If not, it registers the hello
command with the Discord API using registerCommand
.prompt
command is present in the list. If not, it registers the prompt
command with the Discord API using registerCommand
. The prompt
command has an option to prompt the language model with a text.interactionsCommands
object to map the hello
and prompt
commands to the corresponding functions from the discord llm interactions
module.debugger
when updating the prompt
command, which means that the code will pause and allow the developer to inspect the variables when the command is updated.JSON.stringify
function to compare the options of the prompt
command, which is necessary because the options are objects and cannot be compared directly.map
function to transform the options of the prompt
command when comparing them with the options retrieved from the Discord API.