Compare commits

..

2 Commits

Author SHA1 Message Date
Kevin Dang
496ce43939 Infinite Message Length for Block Messages (#55)
* add: message loop for block messages

* add: infinite message length for block embeds

* update: error message on stream length

* rm: unnecessary import

* update: version increment

* update: embed max length

* update: check off features
2024-04-30 19:33:06 -07:00
Kevin Dang
b5194fa645 Discord Administrator Role Permissions (#54)
* add: admin check for disable

* update: shutoff uses memberPerms now

* rm: superUser env variable

* update: version increment

* rm: admin env in docker and workflow
2024-04-25 11:26:22 -07:00
11 changed files with 94 additions and 35 deletions

View File

@@ -22,6 +22,3 @@ 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', ...]

View File

@@ -37,7 +37,6 @@ 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
# set -e ensures if nohup fails, this section fails # set -e ensures if nohup fails, this section fails
- name: Startup Discord Bot Client - name: Startup Discord Bot Client
@@ -68,7 +67,6 @@ 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: |

View File

@@ -16,9 +16,9 @@ The project aims to:
* [x] Containerization with Docker * [x] Containerization with Docker
* [x] Slash Commands Compatible * [x] Slash Commands Compatible
* [x] Generated Token Length Handling for >2000 ~~or >6000 characters~~ * [x] Generated Token Length Handling for >2000 ~~or >6000 characters~~
* [ ] Token Length Handling of any message size * [x] Token Length Handling of any message size
* [ ] External WebUI Integration * [ ] External WebUI Integration
* [ ] Administrator Role Compatible * [x] Administrator Role Compatible
* [ ] Allow others to create their own models personalized for their own servers! * [ ] Allow others to create their own models personalized for their own servers!
* [ ] Documentation on creating your own LLM * [ ] Documentation on creating your own LLM
* [ ] Documentation on web scrapping and cleaning * [ ] Documentation on web scrapping and cleaning

View File

@@ -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.4.2 image: discord/bot:0.4.4
environment: environment:
CLIENT_TOKEN: ${CLIENT_TOKEN} CLIENT_TOKEN: ${CLIENT_TOKEN}
GUILD_ID: ${GUILD_ID} GUILD_ID: ${GUILD_ID}
@@ -17,7 +17,6 @@ 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
View File

@@ -1,12 +1,12 @@
{ {
"name": "discord-ollama", "name": "discord-ollama",
"version": "0.4.0", "version": "0.4.4",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "discord-ollama", "name": "discord-ollama",
"version": "0.4.0", "version": "0.4.4",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"discord.js": "^14.14.1", "discord.js": "^14.14.1",

View File

@@ -1,6 +1,6 @@
{ {
"name": "discord-ollama", "name": "discord-ollama",
"version": "0.4.2", "version": "0.4.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",

View File

@@ -4,7 +4,7 @@ import { openFile } from '../utils/jsonHandler.js'
export const Disable: SlashCommand = { export const Disable: SlashCommand = {
name: 'toggle-chat', name: 'toggle-chat',
description: 'toggle all chat features, slash commands will still work.', description: 'toggle all chat features, Adminstrator Only.',
// set available user options to pass to the command // set available user options to pass to the command
options: [ options: [
@@ -22,6 +22,15 @@ export const Disable: SlashCommand = {
const channel = await client.channels.fetch(interaction.channelId) const channel = await client.channels.fetch(interaction.channelId)
if (!channel || channel.type !== ChannelType.GuildText) return if (!channel || channel.type !== ChannelType.GuildText) return
// check if runner is an admin
if (!interaction.memberPermissions?.has('Administrator')) {
interaction.reply({
content: `${interaction.commandName} is an Administrator Command.\n\nYou, ${interaction.member?.user.username}, are not an Administrator in this server.\nPlease contact an admin to use this command.`,
ephemeral: true
})
return
}
// set state of bot chat features // set state of bot chat features
openFile('config.json', interaction.commandName, interaction.options.get('enabled')?.value) openFile('config.json', interaction.commandName, interaction.options.get('enabled')?.value)

View File

@@ -1,10 +1,9 @@
import { ChannelType, Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js' import { ChannelType, Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js'
import { SlashCommand } from '../utils/commands.js' import { SlashCommand } from '../utils/commands.js'
import Keys from '../keys.js'
export const Shutoff: SlashCommand = { export const Shutoff: SlashCommand = {
name: 'shutoff', name: 'shutoff',
description: 'shutdown the bot. You will need to manually bring it online again.', description: 'shutdown the bot. You will need to manually bring it online again. Administrator Only.',
// set available user options to pass to the command // set available user options to pass to the command
options: [ options: [
@@ -25,24 +24,22 @@ export const Shutoff: SlashCommand = {
// log this, this will probably be improtant for logging who did this // log this, this will probably be improtant for logging who did this
console.log(`User -> ${interaction.user.tag} attempting to shutdown ${client.user!!.tag}`) 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 // check if admin or false on shutdown
if (interaction.user.tag !in superUsers) { if (!interaction.memberPermissions?.has('Administrator')) {
interaction.reply({ interaction.reply({
content: `Shutdown failed:\n\n${interaction.user.tag}, You do not have permission to shutoff **${client.user?.tag}**.`, content: `**Shutdown Aborted:**\n\n${interaction.user.tag}, You do not have permission to shutoff **${client.user?.tag}**.`,
ephemeral: true ephemeral: true
}) })
return // stop from shutting down return // stop from shutting down
} else if (!interaction.options.get('are-you-sure')?.value) { } else if (!interaction.options.get('are-you-sure')?.value) {
interaction.reply({ interaction.reply({
content: `Shutdown failed:\n\n${interaction.user.tag}, You didn't want to shutoff **${client.user?.tag}**.`, content: `**Shutdown Aborted:**\n\n${interaction.user.tag}, You didn't want to shutoff **${client.user?.tag}**.`,
ephemeral: true ephemeral: true
}) })
return return // chickened out
} }
// Shutoff cleared, do it
interaction.reply({ interaction.reply({
content: `${client.user?.tag} is ${interaction.options.get('are-you-sure')?.value ? "shutting down now." : "not shutting down." }`, content: `${client.user?.tag} is ${interaction.options.get('are-you-sure')?.value ? "shutting down now." : "not shutting down." }`,
ephemeral: true ephemeral: true

View File

@@ -8,7 +8,6 @@ export const Keys = {
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

View File

@@ -47,27 +47,72 @@ export async function embedMessage(
for await (const portion of response) { for await (const portion of response) {
result += portion.message.content 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
message.channel.send({ embeds: [errorEmbed] })
break // cancel loop and stop
}
// new embed per token... // new embed per token...
const newEmbed = new EmbedBuilder() const streamEmbed = new EmbedBuilder()
.setTitle(`Responding to ${message.author.tag}`) .setTitle(`Responding to ${message.author.tag}`)
.setDescription(result || 'No Content Yet...') .setDescription(result || 'No Content Yet...')
.setColor('#00FF00') .setColor('#00FF00')
// edit the message // edit the message
sentMessage.edit({ embeds: [newEmbed] }) sentMessage.edit({ embeds: [streamEmbed] })
} }
} else { } else {
response = await blockResponse(params) response = await blockResponse(params)
result = response.message.content result = response.message.content
// only need to create 1 embed again // long message, split into different embeds sadly.
const newEmbed = new EmbedBuilder() if (result.length > 5000) {
.setTitle(`Responding to ${message.author.tag}`) const firstEmbed = new EmbedBuilder()
.setDescription(result || 'No Content to Provide...') .setTitle(`Responding to ${message.author.tag}`)
.setColor('#00FF00') .setDescription(result.slice(0, 5000) || 'No Content to Provide...')
.setColor('#00FF00')
// edit the message // replace first embed
sentMessage.edit({ embeds: [newEmbed] }) 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')
message.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
message.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) { } catch(error: any) {
console.log(`[Util: messageEmbed] Error creating message: ${error.message}`) console.log(`[Util: messageEmbed] Error creating message: ${error.message}`)

View File

@@ -38,6 +38,12 @@ export async function normalMessage(
// append token to message // append token to message
result += portion.message.content result += portion.message.content
// exceeds handled length
if (result.length > 2000) {
message.channel.send(`Response length ${result.length} has exceeded Discord maximum.\n\nLong Stream messages not supported.`)
break // stop stream
}
// resent current output, THIS WILL BE SLOW due to discord limits! // resent current output, THIS WILL BE SLOW due to discord limits!
sentMessage.edit(result || 'No Content Yet...') sentMessage.edit(result || 'No Content Yet...')
} }
@@ -49,8 +55,17 @@ export async function normalMessage(
// check if message length > discord max for normal messages // check if message length > discord max for normal messages
if (result.length > 2000) { if (result.length > 2000) {
sentMessage.edit(result.slice(0, 2000)) sentMessage.edit(result.slice(0, 2000))
message.channel.send(result.slice(2000)) result = result.slice(2000)
} else // edit the 'generic' response to new message
// handle for rest of message that is >2000
while (result.length > 2000) {
message.channel.send(result.slice(0, 2000))
result = result.slice(2000)
}
// last part of message
message.channel.send(result)
} else // edit the 'generic' response to new message since <2000
sentMessage.edit(result) sentMessage.edit(result)
} }
} catch(error: any) { } catch(error: any) {
@@ -59,6 +74,6 @@ export async function normalMessage(
} }
}) })
// return the string representation of response // return the string representation of ollama query response
return result return result
} }