From fe1f7ce5ec9096763811e9f1de0a0d8e6393f6f0 Mon Sep 17 00:00:00 2001 From: Kevin Dang <77701718+kevinthedang@users.noreply.github.com> Date: Fri, 13 Dec 2024 16:55:57 -0800 Subject: [PATCH] Remove Message Style Command (#149) * Remove: Message Style Command * Update: version increment --- docker-compose.yml | 2 +- package-lock.json | 4 +- package.json | 2 +- src/commands/index.ts | 2 - src/commands/messageStyle.ts | 32 --------- src/events/messageCreate.ts | 12 +--- src/utils/configInterfaces.ts | 1 - src/utils/index.ts | 1 - src/utils/messageEmbed.ts | 129 ---------------------------------- tests/commands.test.ts | 2 +- 10 files changed, 8 insertions(+), 179 deletions(-) delete mode 100644 src/commands/messageStyle.ts delete mode 100644 src/utils/messageEmbed.ts diff --git a/docker-compose.yml b/docker-compose.yml index a53401a..fd9b28a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,7 +7,7 @@ services: build: ./ # find docker file in designated path container_name: discord restart: always # rebuild container always - image: kevinthedang/discord-ollama:0.7.4 + image: kevinthedang/discord-ollama:0.7.5 environment: CLIENT_TOKEN: ${CLIENT_TOKEN} OLLAMA_IP: ${OLLAMA_IP} diff --git a/package-lock.json b/package-lock.json index 432ad8f..fb0eab6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "discord-ollama", - "version": "0.7.4", + "version": "0.7.5", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "discord-ollama", - "version": "0.7.4", + "version": "0.7.5", "license": "ISC", "dependencies": { "discord.js": "^14.16.3", diff --git a/package.json b/package.json index d9cdd57..ad2ab14 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "discord-ollama", - "version": "0.7.4", + "version": "0.7.5", "description": "Ollama Integration into discord", "main": "build/index.js", "exports": "./build/index.js", diff --git a/src/commands/index.ts b/src/commands/index.ts index 85e83ba..b843f0b 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -1,6 +1,5 @@ import { SlashCommand } from '../utils/commands.js' import { ThreadCreate } from './threadCreate.js' -import { MessageStyle } from './messageStyle.js' import { MessageStream } from './messageStream.js' import { Disable } from './disable.js' import { Shutoff } from './shutoff.js' @@ -13,7 +12,6 @@ import { SwitchModel } from './switchModel.js' export default [ ThreadCreate, PrivateThreadCreate, - MessageStyle, MessageStream, Disable, Shutoff, diff --git a/src/commands/messageStyle.ts b/src/commands/messageStyle.ts deleted file mode 100644 index 955dcd2..0000000 --- a/src/commands/messageStyle.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js' -import { openConfig, SlashCommand, UserCommand } from '../utils/index.js' - -export const MessageStyle: SlashCommand = { - name: 'message-style', - description: 'sets the message style to embed or normal', - - // set available user options to pass to the command - options: [ - { - name: 'embed', - description: 'toggle embedded or normal message', - type: ApplicationCommandOptionType.Boolean, - required: true - } - ], - - // Query for message information and set the style - run: async (client: Client, interaction: CommandInteraction) => { - // fetch channel and message - const channel = await client.channels.fetch(interaction.channelId) - if (!channel || !UserCommand.includes(channel.type)) return - - // set the message style - openConfig(`${interaction.user.username}-config.json`, interaction.commandName, interaction.options.get('embed')?.value) - - interaction.reply({ - content: `Message style preferences for embed set to: \`${interaction.options.get('embed')?.value}\``, - ephemeral: true - }) - } -} \ No newline at end of file diff --git a/src/events/messageCreate.ts b/src/events/messageCreate.ts index cc3d5a6..4c5b7a2 100644 --- a/src/events/messageCreate.ts +++ b/src/events/messageCreate.ts @@ -1,5 +1,5 @@ import { TextChannel } from 'discord.js' -import { embedMessage, event, Events, normalMessage, UserMessage, clean } from '../utils/index.js' +import { event, Events, normalMessage, UserMessage, clean } from '../utils/index.js' import { getChannelInfo, getServerConfig, getUserConfig, openChannelInfo, openConfig, UserConfig, getAttachmentData } from '../utils/index.js' /** @@ -134,9 +134,6 @@ export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client }) } - // response string for ollama to put its response - let response: string - if (!userConfig) throw new Error(`Failed to initialize User Preference for **${message.author.username}**.\n\nIt's likely you do not have a model set. Please use the \`switch-model\` command to do that.`) @@ -157,11 +154,8 @@ export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client images: messageAttachment || [] }) - // undefined or false, use normal, otherwise use embed - if (userConfig.options['message-style']) - response = await embedMessage(message, ollama, model, msgHist, shouldStream) - else - response = await normalMessage(message, ollama, model, msgHist, shouldStream) + // response string for ollama to put its response + const response: string = await normalMessage(message, ollama, model, msgHist, shouldStream) // If something bad happened, remove user query and stop if (response == undefined) { msgHist.pop(); return } diff --git a/src/utils/configInterfaces.ts b/src/utils/configInterfaces.ts index 2eca167..98bb2e1 100644 --- a/src/utils/configInterfaces.ts +++ b/src/utils/configInterfaces.ts @@ -3,7 +3,6 @@ import { UserMessage } from './index.js' export interface UserConfiguration { 'message-stream'?: boolean, - 'message-style'?: boolean, 'modify-capacity': number, 'switch-model': string } diff --git a/src/utils/index.ts b/src/utils/index.ts index 846316f..13efca8 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,7 +1,6 @@ // Centralized import index export * from './env.js' export * from './events.js' -export * from './messageEmbed.js' export * from './messageNormal.js' export * from './commands.js' export * from './configInterfaces.js' diff --git a/src/utils/messageEmbed.ts b/src/utils/messageEmbed.ts deleted file mode 100644 index f0fa239..0000000 --- a/src/utils/messageEmbed.ts +++ /dev/null @@ -1,129 +0,0 @@ -import { EmbedBuilder, Message, SendableChannels } from 'discord.js' -import { ChatResponse, Ollama } from 'ollama' -import { ChatParams, UserMessage, streamResponse, blockResponse } from './index.js' -import { Queue } from '../queues/queue.js' -import { AbortableAsyncIterator } from 'ollama/src/utils.js' - -/** - * Method to send replies as normal text on discord like any other user - * @param message message sent by the user - * @param model name of model to run query - * @param msgHist message history between user and model - */ -export async function embedMessage( - message: Message, - ollama: Ollama, - model: string, - msgHist: Queue, - stream: boolean -): Promise { - // bot response - let response: ChatResponse | AbortableAsyncIterator - let result: string = '' - - // initial message to client - const botMessage = new EmbedBuilder() - .setTitle(`Responding to ${message.author.tag}`) - .setDescription('Generating Response . . .') - .setColor('#00FF00') - - // send the message - const channel = message.channel as SendableChannels - const sentMessage = await channel.send({ embeds: [botMessage] }) - - // create params - const params: ChatParams = { - model: model, - ollama: ollama, - msgHist: msgHist.getItems() - } - - try { - // check if embed needs to stream - if (stream) { - response = await streamResponse(params) - - for await (const portion of response) { - result += portion.message.content - - // exceeds handled length - if (result.length > 5000) { - const errorEmbed = new EmbedBuilder() - .setTitle(`Responding to ${message.author.tag}`) - .setDescription(`Response length ${result.length} has exceeded Discord maximum.\n\nLong Stream messages not supported.`) - .setColor('#00FF00') - - // send error - channel.send({ embeds: [errorEmbed] }) - break // cancel loop and stop - } - - // new embed per token... - const streamEmbed = new EmbedBuilder() - .setTitle(`Responding to ${message.author.tag}`) - .setDescription(result || 'No Content Yet...') - .setColor('#00FF00') - - // edit the message - sentMessage.edit({ embeds: [streamEmbed] }) - } - } else { - response = await blockResponse(params) - result = response.message.content - - // long message, split into different embeds sadly. - if (result.length > 5000) { - const firstEmbed = new EmbedBuilder() - .setTitle(`Responding to ${message.author.tag}`) - .setDescription(result.slice(0, 5000) || 'No Content to Provide...') - .setColor('#00FF00') - - // replace first embed - sentMessage.edit({ embeds: [firstEmbed] }) - - // take the rest out - result = result.slice(5000) - - // handle the rest - while (result.length > 5000) { - const whileEmbed = new EmbedBuilder() - .setTitle(`Responding to ${message.author.tag}`) - .setDescription(result.slice(0, 5000) || 'No Content to Provide...') - .setColor('#00FF00') - - channel.send({ embeds: [whileEmbed] }) - result = result.slice(5000) - } - - const lastEmbed = new EmbedBuilder() - .setTitle(`Responding to ${message.author.tag}`) - .setDescription(result || 'No Content to Provide...') - .setColor('#00FF00') - - // rest of the response - channel.send({ embeds: [lastEmbed] }) - } else { - // only need to create 1 embed, handles 6000 characters - const newEmbed = new EmbedBuilder() - .setTitle(`Responding to ${message.author.tag}`) - .setDescription(result || 'No Content to Provide...') - .setColor('#00FF00') - - // edit the message - sentMessage.edit({ embeds: [newEmbed] }) - } - } - } catch(error: any) { - console.log(`[Util: messageEmbed] Error creating message: ${error.message}`) - const errorEmbed = new EmbedBuilder() - .setTitle(`Responding to ${message.author.tag}`) - .setDescription(`**Response generation failed.**\n\nReason: ${error.message}`) - .setColor('#00FF00') - - // send back error - sentMessage.edit({ embeds: [errorEmbed] }) - } - - // Hope there is a response! undefined otherwie - return result -} \ No newline at end of file diff --git a/tests/commands.test.ts b/tests/commands.test.ts index 58699f8..a9622ea 100644 --- a/tests/commands.test.ts +++ b/tests/commands.test.ts @@ -22,7 +22,7 @@ describe('Commands Existence', () => { // test specific commands in the object it('references specific commands', () => { const commandsString = commands.map(e => e.name).join(', ') - expect(commandsString).toBe('thread, private-thread, message-style, message-stream, toggle-chat, shutoff, modify-capacity, clear-user-channel-history, pull-model, switch-model') + expect(commandsString).toBe('thread, private-thread, message-stream, toggle-chat, shutoff, modify-capacity, clear-user-channel-history, pull-model, switch-model') }) })