From 5061dab3357d5ca7e11817bcffeb7c4d7e275ff6 Mon Sep 17 00:00:00 2001 From: Kevin Dang <77701718+kevinthedang@users.noreply.github.com> Date: Sun, 6 Oct 2024 18:57:39 -0700 Subject: [PATCH] Remove CLIENT_UID Environment Variable (#123) * Remove: Client UID References * Update: version increment --- .env.sample | 3 --- .github/workflows/build.yml | 2 -- .github/workflows/coverage.yml | 1 - .github/workflows/release.yml | 1 - .github/workflows/test.yml | 1 - docker-compose.yml | 3 +-- docs/setup-discord-app.md | 4 ---- docs/setup-local.md | 2 +- package-lock.json | 4 ++-- package.json | 2 +- src/events/messageCreate.ts | 10 ++++++---- src/keys.ts | 1 - src/utils/events.ts | 3 --- src/utils/mentionClean.ts | 4 ++-- tests/mentionClean.test.ts | 9 ++++++--- 15 files changed, 19 insertions(+), 31 deletions(-) diff --git a/.env.sample b/.env.sample index de90f10..ea37f33 100644 --- a/.env.sample +++ b/.env.sample @@ -4,9 +4,6 @@ CLIENT_TOKEN = BOT_TOKEN # model for the bot to query from (i.e. llama2 [llama2:13b], mistral, codellama, etc... ) MODEL = MODEL_NAME -# discord bot user id for mentions -CLIENT_UID = BOT_USER_ID - # ip/port address of docker container, I use 172.18.0.3 for docker, 127.0.0.1 for local OLLAMA_IP = IP_ADDRESS OLLAMA_PORT = PORT diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 392ef59..2946246 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -32,7 +32,6 @@ jobs: touch .env echo CLIENT_TOKEN = ${{ secrets.BOT_TOKEN }} >> .env echo MODEL = ${{ secrets.MODEL }} >> .env - echo CLIENT_UID = ${{ secrets.CLIENT_UID }} >> .env echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env @@ -60,7 +59,6 @@ jobs: touch .env echo CLIENT_TOKEN = ${{ secrets.BOT_TOKEN }} >> .env echo MODEL = ${{ secrets.MODEL }} >> .env - echo CLIENT_UID = ${{ secrets.CLIENT_UID }} >> .env echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index bd20715..03255e6 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -29,7 +29,6 @@ jobs: touch .env echo CLIENT_TOKEN = ${{ secrets.BOT_TOKEN }} >> .env echo MODEL = ${{ secrets.MODEL }} >> .env - echo CLIENT_UID = ${{ secrets.CLIENT_UID }} >> .env echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 32fd158..946487a 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -25,7 +25,6 @@ jobs: touch .env echo CLIENT_TOKEN = NOT_REAL_TOKEN >> .env echo MODEL = ${{ secrets.MODEL }} >> .env - echo CLIENT_UID = ${{ secrets.CLIENT_UID }} >> .env echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6b599f3..8182563 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -40,7 +40,6 @@ jobs: touch .env echo CLIENT_TOKEN = ${{ secrets.BOT_TOKEN }} >> .env echo MODEL = ${{ secrets.MODEL }} >> .env - echo CLIENT_UID = ${{ secrets.CLIENT_UID }} >> .env echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env diff --git a/docker-compose.yml b/docker-compose.yml index c2303ba..1a980eb 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,11 +7,10 @@ services: build: ./ # find docker file in designated path container_name: discord restart: always # rebuild container always - image: kevinthedang/discord-ollama:0.6.1 + image: kevinthedang/discord-ollama:0.6.2 environment: CLIENT_TOKEN: ${CLIENT_TOKEN} MODEL: ${MODEL} - CLIENT_UID: ${CLIENT_UID} OLLAMA_IP: ${OLLAMA_IP} OLLAMA_PORT: ${OLLAMA_PORT} networks: diff --git a/docs/setup-discord-app.md b/docs/setup-discord-app.md index 481b88d..ab36f9a 100644 --- a/docs/setup-discord-app.md +++ b/docs/setup-discord-app.md @@ -18,10 +18,6 @@ ![Token](../imgs/tutorial/token.png) -* You will also need your App's **Client ID**, navigate to **OAuth2** and copy your id. - -![Client Id](../imgs/tutorial/client-id.png) - * That should be all of the environment variables needed from Discord, now we need this app on your server. * Navigate to **Installation** and Copy the provided **Install Link** to allow your App to your server. * You should set the **Guild Install** permissions as you like, for this purpose we will allow admin priviledges for now. Ensure the **bot** scope is added to do this. diff --git a/docs/setup-local.md b/docs/setup-local.md index 2ea885b..f05e1a7 100644 --- a/docs/setup-local.md +++ b/docs/setup-local.md @@ -14,7 +14,7 @@ ## To Run Locally (without Docker) * Run `npm install` to install the npm packages. * Ensure that your [.env](../.env.sample) file's `OLLAMA_IP` is `127.0.0.1` to work properly. - * You only need your `CLIENT_TOKEN`, `MODEL`, `CLIENT_UID`, `OLLAMA_IP`, `OLLAMA_PORT`. + * You only need your `CLIENT_TOKEN`, `MODEL`, `OLLAMA_IP`, `OLLAMA_PORT`. * The ollama ip and port should just use it's defaults by nature. If not, utilize `OLLAMA_IP = 127.0.0.1` and `OLLAMA_PORT = 11434`. * Now, you can run the bot by running `npm run client` which will build and run the decompiled typescript and run the setup for ollama. * **IMPORTANT**: This must be ran in the wsl/Linux instance to work properly! Using Command Prompt/Powershell/Git Bash/etc. will not work on Windows (at least in my experience). diff --git a/package-lock.json b/package-lock.json index a8caad8..93b1a58 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "discord-ollama", - "version": "0.6.1", + "version": "0.6.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "discord-ollama", - "version": "0.6.1", + "version": "0.6.2", "license": "ISC", "dependencies": { "discord.js": "^14.15.3", diff --git a/package.json b/package.json index 39650bf..a485eed 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "discord-ollama", - "version": "0.6.1", + "version": "0.6.2", "description": "Ollama Integration into discord", "main": "build/index.js", "exports": "./build/index.js", diff --git a/src/events/messageCreate.ts b/src/events/messageCreate.ts index b9950dc..4f38f4d 100644 --- a/src/events/messageCreate.ts +++ b/src/events/messageCreate.ts @@ -8,14 +8,16 @@ import { getChannelInfo, getServerConfig, getUserConfig, openChannelInfo, openCo * * @param message the message received from the channel */ -export default event(Events.MessageCreate, async ({ log, msgHist, tokens, ollama }, message) => { - log(`Message \"${clean(message.content)}\" from ${message.author.tag} in channel/thread ${message.channelId}.`) +export default event(Events.MessageCreate, async ({ log, msgHist, tokens, ollama, client }, message) => { + const clientId = client.user!!.id + const cleanedMessage = clean(message.content, clientId) + log(`Message \"${cleanedMessage}\" from ${message.author.tag} in channel/thread ${message.channelId}.`) // Do not respond if bot talks in the chat if (message.author.username === message.client.user.username) return // Only respond if message mentions the bot - if (!message.mentions.has(tokens.clientUid)) return + if (!message.mentions.has(clientId)) return // default stream to false let shouldStream = false @@ -113,7 +115,7 @@ export default event(Events.MessageCreate, async ({ log, msgHist, tokens, ollama // push user response before ollama query msgHist.enqueue({ role: 'user', - content: clean(message.content), + content: cleanedMessage, images: messageAttachment || [] }) diff --git a/src/keys.ts b/src/keys.ts index 0b9be4d..5f97ccc 100644 --- a/src/keys.ts +++ b/src/keys.ts @@ -3,7 +3,6 @@ import { getEnvVar } from './utils/index.js' export const Keys = { clientToken: getEnvVar('CLIENT_TOKEN'), model: getEnvVar('MODEL'), - clientUid: getEnvVar('CLIENT_UID'), ipAddress: getEnvVar('OLLAMA_IP'), portAddress: getEnvVar('OLLAMA_PORT'), } as const // readonly keys diff --git a/src/utils/events.ts b/src/utils/events.ts index 0ff02bc..e04c8b9 100644 --- a/src/utils/events.ts +++ b/src/utils/events.ts @@ -10,13 +10,10 @@ export type EventKeys = keyof ClientEvents // only wants keys of ClientEvents ob /** * Tokens to run the bot as intended - * @param channel the channel where the bot will respond to queries * @param model chosen model for the ollama to utilize - * @param clientUid the discord id for the bot */ export type Tokens = { model: string, - clientUid: string } /** diff --git a/src/utils/mentionClean.ts b/src/utils/mentionClean.ts index 884fa82..2444085 100644 --- a/src/utils/mentionClean.ts +++ b/src/utils/mentionClean.ts @@ -10,7 +10,7 @@ import Keys from "../keys.js" * @param message * @returns message without client id */ -export function clean(message: string): string { - const cleanedMessage: string = message.replace(`<@${Keys.clientUid}>`, '').trim() +export function clean(message: string, clientId: string): string { + const cleanedMessage: string = message.replace(`<@${clientId}>`, '').trim() return cleanedMessage } \ No newline at end of file diff --git a/tests/mentionClean.test.ts b/tests/mentionClean.test.ts index eb06931..c225c2c 100644 --- a/tests/mentionClean.test.ts +++ b/tests/mentionClean.test.ts @@ -1,5 +1,8 @@ import { describe, expect, it } from 'vitest' -import { getEnvVar, clean } from '../src/utils/index.js' +import { clean } from '../src/utils/index.js' + +// Sample UID for testing +const sampleId = '123456789' /** * MentionClean test suite, tests the clean function @@ -10,7 +13,7 @@ import { getEnvVar, clean } from '../src/utils/index.js' describe('Mentions Cleaned', () => { // test for id removal from message it('removes the mention from a message', () => { - const message = `<@${getEnvVar('CLIENT_UID')}> Hello, World!` - expect(clean(message)).toBe('Hello, World!') + const message = `<@${sampleId}> Hello, World!` + expect(clean(message, sampleId)).toBe('Hello, World!') }) }) \ No newline at end of file