mirror of
https://github.com/kevinthedang/discord-ollama.git
synced 2025-12-12 19:56:06 -05:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5f8b513269 | ||
|
|
fcb0267559 | ||
|
|
6b903cff5e |
@@ -21,4 +21,7 @@ OLLAMA_PORT = PORT
|
|||||||
DISCORD_IP = IP_ADDRESS
|
DISCORD_IP = IP_ADDRESS
|
||||||
|
|
||||||
# subnet address, ex. 172.18.0.0 as we use /16.
|
# subnet address, ex. 172.18.0.0 as we use /16.
|
||||||
SUBNET_ADDRESS = ADDRESS
|
SUBNET_ADDRESS = ADDRESS
|
||||||
|
|
||||||
|
# list of admins to handle admin commands for the bot, use single quotes
|
||||||
|
ADMINS=['username1', 'username2', 'username3', ...]
|
||||||
6
.github/workflows/build-test.yml
vendored
6
.github/workflows/build-test.yml
vendored
@@ -1,4 +1,4 @@
|
|||||||
name: Test Discord-Ollama Builds
|
name: Builds
|
||||||
run-name: Validate Node and Docker Builds
|
run-name: Validate Node and Docker Builds
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
@@ -65,6 +65,7 @@ jobs:
|
|||||||
echo CLIENT_UID = ${{ secrets.CLIENT_UID }} >> .env
|
echo CLIENT_UID = ${{ secrets.CLIENT_UID }} >> .env
|
||||||
echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env
|
echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env
|
||||||
echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env
|
echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env
|
||||||
|
echo ADMINS = ${{ secrets.ADMINS }} >> .env
|
||||||
|
|
||||||
- name: Setup Docker Network and Images
|
- name: Setup Docker Network and Images
|
||||||
run: |
|
run: |
|
||||||
@@ -74,8 +75,7 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
(docker images | grep -q 'discord/bot' && docker images | grep -qE 'ollama/ollama') || exit 1
|
(docker images | grep -q 'discord/bot' && docker images | grep -qE 'ollama/ollama') || exit 1
|
||||||
|
|
||||||
|
- name: Check Containers Exist
|
||||||
- name: Check Images Exist
|
|
||||||
run: |
|
run: |
|
||||||
(docker ps | grep -q 'ollama' && docker ps | grep -q 'discord') || exit 1
|
(docker ps | grep -q 'ollama' && docker ps | grep -q 'discord') || exit 1
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
# Discord Ollama Integration [](https://creativecommons.org/licenses/by-nc/4.0/) [](https://github.com/kevinthedang/discord-ollama/releases/latest)
|
# Discord Ollama Integration [](https://creativecommons.org/licenses/by-nc/4.0/) [](https://github.com/kevinthedang/discord-ollama/releases/latest)
|
||||||
Ollama is an AI model management tool that allows users to install and use custom large language models locally. The goal is to create a discord bot that will utilize Ollama and chat with it on a Discord!
|
Ollama is an AI model management tool that allows users to install and use custom large language models locally. The goal is to create a discord bot that will utilize Ollama and chat with it on a Discord server! Also, allow others to create their own models personalized for their own servers!
|
||||||
|
|
||||||
## Environment Setup
|
## Environment Setup
|
||||||
* Clone this repo using `git clone https://github.com/kevinthedang/discord-ollama.git` or just use [GitHub Desktop](https://desktop.github.com/) to clone the repo.
|
* Clone this repo using `git clone https://github.com/kevinthedang/discord-ollama.git` or just use [GitHub Desktop](https://desktop.github.com/) to clone the repo.
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ services:
|
|||||||
build: ./ # find docker file in designated path
|
build: ./ # find docker file in designated path
|
||||||
container_name: discord
|
container_name: discord
|
||||||
restart: always # rebuild container always
|
restart: always # rebuild container always
|
||||||
image: discord/bot:0.2.0
|
image: discord/bot:0.3.4
|
||||||
environment:
|
environment:
|
||||||
CLIENT_TOKEN: ${CLIENT_TOKEN}
|
CLIENT_TOKEN: ${CLIENT_TOKEN}
|
||||||
GUILD_ID: ${GUILD_ID}
|
GUILD_ID: ${GUILD_ID}
|
||||||
@@ -17,6 +17,7 @@ services:
|
|||||||
CLIENT_UID: ${CLIENT_UID}
|
CLIENT_UID: ${CLIENT_UID}
|
||||||
OLLAMA_IP: ${OLLAMA_IP}
|
OLLAMA_IP: ${OLLAMA_IP}
|
||||||
OLLAMA_PORT: ${OLLAMA_PORT}
|
OLLAMA_PORT: ${OLLAMA_PORT}
|
||||||
|
ADMINS: ${ADMINS}
|
||||||
networks:
|
networks:
|
||||||
ollama-net:
|
ollama-net:
|
||||||
ipv4_address: ${DISCORD_IP}
|
ipv4_address: ${DISCORD_IP}
|
||||||
|
|||||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "discord-ollama",
|
"name": "discord-ollama",
|
||||||
"version": "0.3.2",
|
"version": "0.3.4",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "discord-ollama",
|
"name": "discord-ollama",
|
||||||
"version": "0.3.2",
|
"version": "0.3.4",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"axios": "^1.6.2",
|
"axios": "^1.6.2",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "discord-ollama",
|
"name": "discord-ollama",
|
||||||
"version": "0.3.2",
|
"version": "0.3.4",
|
||||||
"description": "Ollama Integration into discord",
|
"description": "Ollama Integration into discord",
|
||||||
"main": "build/index.js",
|
"main": "build/index.js",
|
||||||
"exports": "./build/index.js",
|
"exports": "./build/index.js",
|
||||||
@@ -10,14 +10,14 @@
|
|||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
"prod": "node .",
|
"prod": "node .",
|
||||||
"client": "npm run build && npm run prod",
|
"client": "npm run build && npm run prod",
|
||||||
"clean": "docker compose down && docker rmi $(docker images | grep 0.2.0 | tr -s ' ' | cut -d ' ' -f 3) && docker rmi $(docker images --filter \"dangling=true\" -q --no-trunc)",
|
"clean": "docker compose down && docker rmi $(docker images | grep $(node -p \"require('./package.json').version\") | tr -s ' ' | cut -d ' ' -f 3) && docker rmi $(docker images --filter \"dangling=true\" -q --no-trunc)",
|
||||||
"start": "docker compose build --no-cache && docker compose up -d",
|
"start": "docker compose build --no-cache && docker compose up -d",
|
||||||
"docker:start": "npm run docker:network && npm run docker:build && npm run docker:client && npm run docker:ollama",
|
"docker:start": "npm run docker:network && npm run docker:build && npm run docker:client && npm run docker:ollama",
|
||||||
"docker:start-cpu": "npm run docker:network && npm run docker:build && npm run docker:client && npm run docker:ollama-cpu",
|
"docker:start-cpu": "npm run docker:network && npm run docker:build && npm run docker:client && npm run docker:ollama-cpu",
|
||||||
"docker:clean": "docker rmi $(docker images --filter \"dangling=true\" -q --no-trunc)",
|
"docker:clean": "docker rmi $(docker images --filter \"dangling=true\" -q --no-trunc)",
|
||||||
"docker:network": "docker network create --subnet=172.18.0.0/16 ollama-net",
|
"docker:network": "docker network create --subnet=172.18.0.0/16 ollama-net",
|
||||||
"docker:build": "docker build --no-cache -t discord/bot:0.3.2 .",
|
"docker:build": "docker build --no-cache -t discord/bot:$(node -p \"require('./package.json').version\") .",
|
||||||
"docker:client": "docker run -d -v discord:/src/app --name discord --network ollama-net --ip 172.18.0.3 discord/bot:0.3.2",
|
"docker:client": "docker run -d -v discord:/src/app --name discord --network ollama-net --ip 172.18.0.3 discord/bot:$(node -p \"require('./package.json').version\")",
|
||||||
"docker:ollama": "docker run -d --gpus=all -v ollama:/root/.ollama -p 11434:11434 --name ollama --network ollama-net --ip 172.18.0.2 ollama/ollama:latest",
|
"docker:ollama": "docker run -d --gpus=all -v ollama:/root/.ollama -p 11434:11434 --name ollama --network ollama-net --ip 172.18.0.2 ollama/ollama:latest",
|
||||||
"docker:ollama-cpu": "docker run -d -v ollama:/root/.ollama -p 11434:11434 --name ollama --network ollama-net --ip 172.18.0.2 ollama/ollama:latest"
|
"docker:ollama-cpu": "docker run -d -v ollama:/root/.ollama -p 11434:11434 --name ollama --network ollama-net --ip 172.18.0.2 ollama/ollama:latest"
|
||||||
},
|
},
|
||||||
|
|||||||
33
src/commands/disable.ts
Normal file
33
src/commands/disable.ts
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import { ChannelType, Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js'
|
||||||
|
import { SlashCommand } from '../utils/commands.js'
|
||||||
|
import { openFile } from '../utils/jsonHandler.js'
|
||||||
|
|
||||||
|
export const Disable: SlashCommand = {
|
||||||
|
name: 'toggle-chat',
|
||||||
|
description: 'toggle all chat features, slash commands will still work.',
|
||||||
|
|
||||||
|
// set available user options to pass to the command
|
||||||
|
options: [
|
||||||
|
{
|
||||||
|
name: 'enabled',
|
||||||
|
description: 'true = enabled, false = disabled',
|
||||||
|
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 || channel.type !== ChannelType.GuildText) return
|
||||||
|
|
||||||
|
// set state of bot chat features
|
||||||
|
openFile('config.json', interaction.commandName, interaction.options.get('enabled')?.value)
|
||||||
|
|
||||||
|
interaction.reply({
|
||||||
|
content: `Chat features has been \`${interaction.options.get('enabled')?.value ? "enabled" : "disabled" }\``,
|
||||||
|
ephemeral: true
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,9 +2,13 @@ import { SlashCommand } from '../utils/commands.js'
|
|||||||
import { ThreadCreate } from './threadCreate.js'
|
import { ThreadCreate } from './threadCreate.js'
|
||||||
import { MessageStyle } from './messageStyle.js'
|
import { MessageStyle } from './messageStyle.js'
|
||||||
import { MessageStream } from './messageStream.js'
|
import { MessageStream } from './messageStream.js'
|
||||||
|
import { Disable } from './disable.js'
|
||||||
|
import { Shutoff } from './shutoff.js'
|
||||||
|
|
||||||
export default [
|
export default [
|
||||||
ThreadCreate,
|
ThreadCreate,
|
||||||
MessageStyle,
|
MessageStyle,
|
||||||
MessageStream
|
MessageStream,
|
||||||
|
Disable,
|
||||||
|
Shutoff
|
||||||
] as SlashCommand[]
|
] as SlashCommand[]
|
||||||
48
src/commands/shutoff.ts
Normal file
48
src/commands/shutoff.ts
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
import { ChannelType, Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js'
|
||||||
|
import { SlashCommand } from '../utils/commands.js'
|
||||||
|
import Keys from '../keys.js'
|
||||||
|
|
||||||
|
export const Shutoff: SlashCommand = {
|
||||||
|
name: 'shutoff',
|
||||||
|
description: 'shutdown the bot. You will need to manually bring it online again.',
|
||||||
|
|
||||||
|
// set available user options to pass to the command
|
||||||
|
options: [
|
||||||
|
{
|
||||||
|
name: 'are-you-sure',
|
||||||
|
description: 'true = yes, false = I\'m scared',
|
||||||
|
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 || channel.type !== ChannelType.GuildText) return
|
||||||
|
|
||||||
|
// log this, this will probably be improtant for logging who did this
|
||||||
|
console.log(`User -> ${interaction.user.tag} attempting to shutdown ${client.user!!.tag}`)
|
||||||
|
|
||||||
|
// create list of superUsers based on string parse
|
||||||
|
const superUsers: string[] = JSON.parse(Keys.superUser.replace(/'/g, '"'))
|
||||||
|
|
||||||
|
// check if admin or false on shutdown
|
||||||
|
if (interaction.user.tag in superUsers || !(!interaction.options.get('are-you-sure')?.value && interaction.user.tag in superUsers)) {
|
||||||
|
interaction.reply({
|
||||||
|
content: `Shutdown failed:\n\n${interaction.user.tag}, You do not have permission to shutoff **${client.user?.tag}**, otherwise, you just didn't want to.`,
|
||||||
|
ephemeral: true
|
||||||
|
})
|
||||||
|
return // stop from shutting down
|
||||||
|
}
|
||||||
|
|
||||||
|
interaction.reply({
|
||||||
|
content: `${client.user?.tag} is ${interaction.options.get('are-you-sure')?.value ? "shutting down now." : "not shutting down." }`,
|
||||||
|
ephemeral: true
|
||||||
|
})
|
||||||
|
|
||||||
|
// clean up client instance and stop
|
||||||
|
client.destroy()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import { ChatResponse } from 'ollama'
|
import { ChatResponse } from 'ollama'
|
||||||
import { embedMessage, event, Events, normalMessage } from '../utils/index.js'
|
import { embedMessage, event, Events, normalMessage } from '../utils/index.js'
|
||||||
import { Configuration, getConfig } from '../utils/jsonHandler.js'
|
import { Configuration, getConfig, openFile } from '../utils/jsonHandler.js'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Max Message length for free users is 2000 characters (bot or not).
|
* Max Message length for free users is 2000 characters (bot or not).
|
||||||
@@ -28,8 +28,15 @@ export default event(Events.MessageCreate, async ({ log, msgHist, tokens, ollama
|
|||||||
try {
|
try {
|
||||||
const config: Configuration = await new Promise((resolve, reject) => {
|
const config: Configuration = await new Promise((resolve, reject) => {
|
||||||
getConfig('config.json', (config) => {
|
getConfig('config.json', (config) => {
|
||||||
|
// check if config.json exists
|
||||||
if (config === undefined) {
|
if (config === undefined) {
|
||||||
reject(new Error('No Configuration is set up.'))
|
reject(new Error('No Configuration is set up.\n\nCreating \`config.json\` with \`message-style\` set as \`true\` for embedded messages.\nPlease try chatting again.'))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if chat is disabled
|
||||||
|
if(!config.options['toggle-chat']) {
|
||||||
|
reject(new Error('Admin(s) have disabled chat features.\n\n Please contact your server\'s admin(s).'))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
resolve(config)
|
resolve(config)
|
||||||
@@ -54,6 +61,7 @@ export default event(Events.MessageCreate, async ({ log, msgHist, tokens, ollama
|
|||||||
})
|
})
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
msgHist.pop() // remove message because of failure
|
msgHist.pop() // remove message because of failure
|
||||||
message.reply(`**Response generation failed.**\n\nReason: ${error.message}\n\nPlease use any config slash command.`)
|
openFile('config.json', 'message-style', true)
|
||||||
|
message.reply(`**Response generation failed.**\n\n**Reason:** *${error.message}*`)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -7,7 +7,8 @@ export const Keys = {
|
|||||||
clientUid: getEnvVar('CLIENT_UID'),
|
clientUid: getEnvVar('CLIENT_UID'),
|
||||||
guildId: getEnvVar('GUILD_ID'),
|
guildId: getEnvVar('GUILD_ID'),
|
||||||
ipAddress: getEnvVar('OLLAMA_IP'),
|
ipAddress: getEnvVar('OLLAMA_IP'),
|
||||||
portAddress: getEnvVar('OLLAMA_PORT')
|
portAddress: getEnvVar('OLLAMA_PORT'),
|
||||||
|
superUser: getEnvVar('ADMINS')
|
||||||
} as const // readonly keys
|
} as const // readonly keys
|
||||||
|
|
||||||
export default Keys
|
export default Keys
|
||||||
@@ -4,7 +4,8 @@ export interface Configuration {
|
|||||||
readonly name: string
|
readonly name: string
|
||||||
options: {
|
options: {
|
||||||
'message-stream'?: boolean,
|
'message-stream'?: boolean,
|
||||||
'message-style'?: boolean
|
'message-style'?: boolean,
|
||||||
|
'toggle-chat'?: boolean
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user