Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6ac45afb13 | ||
|
|
d570a50d46 | ||
|
|
1c8449d578 | ||
|
|
33152b33f3 |
@@ -1,6 +1,9 @@
|
|||||||
# Discord token for the bot
|
# Discord token for the bot
|
||||||
CLIENT_TOKEN = BOT_TOKEN
|
CLIENT_TOKEN = BOT_TOKEN
|
||||||
|
|
||||||
|
# Default model for new users
|
||||||
|
MODEL = DEFAULT_MODEL
|
||||||
|
|
||||||
# ip/port address of docker container, I use 172.18.0.3 for docker, 127.0.0.1 for local
|
# 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_IP = IP_ADDRESS
|
||||||
OLLAMA_PORT = PORT
|
OLLAMA_PORT = PORT
|
||||||
|
|||||||
24
CODEOWNERS
Normal file
24
CODEOWNERS
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
# The further along the ownership is, the more precedence it has.
|
||||||
|
# This to make sure the right people look at certain changes.
|
||||||
|
# Last Edited: 11/23/2024
|
||||||
|
# Author: Kevin Dang
|
||||||
|
|
||||||
|
# These owners will be the default owners
|
||||||
|
# for everything in the repo. However it's
|
||||||
|
# only for the rest of the files not declared by the
|
||||||
|
# following ownerships below.
|
||||||
|
* @kevinthedang @JT2M0L3Y
|
||||||
|
|
||||||
|
# Technical/Business Code Ownership
|
||||||
|
/src/ @kevinthedang @JT2M0L3Y
|
||||||
|
/tests/ @kevinthedang @JT2M0L3Y
|
||||||
|
/.github/ @kevinthedang
|
||||||
|
|
||||||
|
# Docker Ownership
|
||||||
|
Dockerfile @kevinthedang
|
||||||
|
docker-compose.yml @kevinthedang
|
||||||
|
|
||||||
|
# Documentation Ownership
|
||||||
|
/docs/ @kevinthedang
|
||||||
|
/imgs/ @kevinthedang
|
||||||
|
|
||||||
@@ -7,7 +7,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: kevinthedang/discord-ollama:0.7.1
|
image: kevinthedang/discord-ollama:0.7.4
|
||||||
environment:
|
environment:
|
||||||
CLIENT_TOKEN: ${CLIENT_TOKEN}
|
CLIENT_TOKEN: ${CLIENT_TOKEN}
|
||||||
OLLAMA_IP: ${OLLAMA_IP}
|
OLLAMA_IP: ${OLLAMA_IP}
|
||||||
|
|||||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "discord-ollama",
|
"name": "discord-ollama",
|
||||||
"version": "0.7.1",
|
"version": "0.7.4",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "discord-ollama",
|
"name": "discord-ollama",
|
||||||
"version": "0.7.1",
|
"version": "0.7.4",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"discord.js": "^14.16.3",
|
"discord.js": "^14.16.3",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "discord-ollama",
|
"name": "discord-ollama",
|
||||||
"version": "0.7.1",
|
"version": "0.7.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",
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ export const ollama = new Ollama({
|
|||||||
const messageHistory: Queue<UserMessage> = new Queue<UserMessage>
|
const messageHistory: Queue<UserMessage> = new Queue<UserMessage>
|
||||||
|
|
||||||
// register all events
|
// register all events
|
||||||
registerEvents(client, Events, messageHistory, ollama)
|
registerEvents(client, Events, messageHistory, ollama, Keys.defaultModel)
|
||||||
|
|
||||||
// Try to log in the client
|
// Try to log in the client
|
||||||
await client.login(Keys.clientToken)
|
await client.login(Keys.clientToken)
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
import { ChannelType, Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js'
|
import { Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js'
|
||||||
import { openConfig, SlashCommand } from '../utils/index.js'
|
import { openConfig, SlashCommand, UserCommand } from '../utils/index.js'
|
||||||
|
|
||||||
export const Capacity: SlashCommand = {
|
export const Capacity: SlashCommand = {
|
||||||
name: 'modify-capacity',
|
name: 'modify-capacity',
|
||||||
description: 'number of messages bot will hold for context.',
|
description: 'maximum amount messages bot will hold for context.',
|
||||||
|
|
||||||
// set available user options to pass to the command
|
// set available user options to pass to the command
|
||||||
options: [
|
options: [
|
||||||
{
|
{
|
||||||
name: 'context-capacity',
|
name: 'context-capacity',
|
||||||
description: 'a number to set capacity',
|
description: 'number of allowed messages to remember',
|
||||||
type: ApplicationCommandOptionType.Number,
|
type: ApplicationCommandOptionType.Number,
|
||||||
required: true
|
required: true
|
||||||
}
|
}
|
||||||
@@ -19,13 +19,13 @@ export const Capacity: SlashCommand = {
|
|||||||
run: async (client: Client, interaction: CommandInteraction) => {
|
run: async (client: Client, interaction: CommandInteraction) => {
|
||||||
// fetch channel and message
|
// fetch channel and message
|
||||||
const channel = await client.channels.fetch(interaction.channelId)
|
const channel = await client.channels.fetch(interaction.channelId)
|
||||||
if (!channel || channel.type !== (ChannelType.PrivateThread && ChannelType.PublicThread && ChannelType.GuildText)) return
|
if (!channel || !UserCommand.includes(channel.type)) return
|
||||||
|
|
||||||
// set state of bot chat features
|
// set state of bot chat features
|
||||||
openConfig(`${interaction.user.username}-config.json`, interaction.commandName, interaction.options.get('context-capacity')?.value)
|
openConfig(`${interaction.user.username}-config.json`, interaction.commandName, interaction.options.get('context-capacity')?.value)
|
||||||
|
|
||||||
interaction.reply({
|
interaction.reply({
|
||||||
content: `Message History Capacity has been set to \`${interaction.options.get('context-capacity')?.value}\``,
|
content: `Max message history is now set to \`${interaction.options.get('context-capacity')?.value}\``,
|
||||||
ephemeral: true
|
ephemeral: true
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,17 @@
|
|||||||
import { ChannelType, Client, CommandInteraction, TextChannel } from 'discord.js'
|
import { Channel, Client, CommandInteraction, TextChannel } from 'discord.js'
|
||||||
import { clearChannelInfo, SlashCommand } from '../utils/index.js'
|
import { clearChannelInfo, SlashCommand, UserCommand } from '../utils/index.js'
|
||||||
|
|
||||||
export const ClearUserChannelHistory: SlashCommand = {
|
export const ClearUserChannelHistory: SlashCommand = {
|
||||||
name: 'clear-user-channel-history',
|
name: 'clear-user-channel-history',
|
||||||
description: 'clears history for user running this command in current channel',
|
description: 'clears history for user in the current channel',
|
||||||
|
|
||||||
// Clear channel history for intended user
|
// Clear channel history for intended user
|
||||||
run: async (client: Client, interaction: CommandInteraction) => {
|
run: async (client: Client, interaction: CommandInteraction) => {
|
||||||
// fetch current channel
|
// fetch current channel
|
||||||
const channel = await client.channels.fetch(interaction.channelId)
|
const channel: Channel | null = await client.channels.fetch(interaction.channelId)
|
||||||
|
|
||||||
// if not an existing channel or a GuildText, fail command
|
// if not an existing channel or a GuildText, fail command
|
||||||
if (!channel || channel.type !== ChannelType.GuildText) return
|
if (!channel || !UserCommand.includes(channel.type)) return
|
||||||
|
|
||||||
// clear channel info for user
|
// clear channel info for user
|
||||||
const successfulWipe = await clearChannelInfo(interaction.channelId,
|
const successfulWipe = await clearChannelInfo(interaction.channelId,
|
||||||
@@ -21,12 +21,12 @@ export const ClearUserChannelHistory: SlashCommand = {
|
|||||||
// check result of clearing history
|
// check result of clearing history
|
||||||
if (successfulWipe)
|
if (successfulWipe)
|
||||||
interaction.reply({
|
interaction.reply({
|
||||||
content: `Channel history in **${channel.name}** cleared for **${interaction.user.username}**.`,
|
content: `History cleared in **this channel** cleared for **${interaction.user.username}**.`,
|
||||||
ephemeral: true
|
ephemeral: true
|
||||||
})
|
})
|
||||||
else
|
else
|
||||||
interaction.reply({
|
interaction.reply({
|
||||||
content: `Channel history could not be found for **${interaction.user.username}** in **${channel.name}**.\n\nPlease chat with **${client.user?.username}** to start a chat history.`,
|
content: `History was not be found for **${interaction.user.username}** in **this channel**.\n\nPlease chat with **${client.user?.username}** to start a chat history.`,
|
||||||
ephemeral: true
|
ephemeral: true
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import { ChannelType, Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js'
|
import { Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js'
|
||||||
import { openConfig, SlashCommand } from '../utils/index.js'
|
import { AdminCommand, openConfig, SlashCommand } from '../utils/index.js'
|
||||||
|
|
||||||
export const Disable: SlashCommand = {
|
export const Disable: SlashCommand = {
|
||||||
name: 'toggle-chat',
|
name: 'toggle-chat',
|
||||||
description: 'toggle all chat features, Adminstrator Only.',
|
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: [
|
||||||
@@ -19,12 +19,12 @@ export const Disable: SlashCommand = {
|
|||||||
run: async (client: Client, interaction: CommandInteraction) => {
|
run: async (client: Client, interaction: CommandInteraction) => {
|
||||||
// fetch channel and message
|
// fetch channel and message
|
||||||
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 || !AdminCommand.includes(channel.type)) return
|
||||||
|
|
||||||
// check if runner is an admin
|
// check if runner is an admin
|
||||||
if (!interaction.memberPermissions?.has('Administrator')) {
|
if (!interaction.memberPermissions?.has('Administrator')) {
|
||||||
interaction.reply({
|
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.`,
|
content: `${interaction.commandName} is an admin command.\n\nPlease contact an admin to use this command for you.`,
|
||||||
ephemeral: true
|
ephemeral: true
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
@@ -34,7 +34,7 @@ export const Disable: SlashCommand = {
|
|||||||
openConfig(`${interaction.guildId}-config.json`, interaction.commandName, interaction.options.get('enabled')?.value)
|
openConfig(`${interaction.guildId}-config.json`, interaction.commandName, interaction.options.get('enabled')?.value)
|
||||||
|
|
||||||
interaction.reply({
|
interaction.reply({
|
||||||
content: `Chat features has been \`${interaction.options.get('enabled')?.value ? "enabled" : "disabled" }\``,
|
content: `${client.user?.username} is now **${interaction.options.get('enabled')?.value ? "enabled" : "disabled" }**.`,
|
||||||
ephemeral: true
|
ephemeral: true
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
import { ApplicationCommandOptionType, ChannelType, Client, CommandInteraction } from 'discord.js'
|
import { ApplicationCommandOptionType, Client, CommandInteraction } from 'discord.js'
|
||||||
import { openConfig, SlashCommand } from '../utils/index.js'
|
import { openConfig, SlashCommand, UserCommand } from '../utils/index.js'
|
||||||
|
|
||||||
export const MessageStream: SlashCommand = {
|
export const MessageStream: SlashCommand = {
|
||||||
name: 'message-stream',
|
name: 'message-stream',
|
||||||
description: 'change preference on message streaming from ollama. WARNING: can be very slow.',
|
description: 'change preference on message streaming from ollama. WARNING: can be very slow due to Discord limits.',
|
||||||
|
|
||||||
// user option(s) for setting stream
|
// user option(s) for setting stream
|
||||||
options: [
|
options: [
|
||||||
{
|
{
|
||||||
name: 'stream',
|
name: 'stream',
|
||||||
description: 'enable or disable stream preference',
|
description: 'enable or disable message streaming',
|
||||||
type: ApplicationCommandOptionType.Boolean,
|
type: ApplicationCommandOptionType.Boolean,
|
||||||
required: true
|
required: true
|
||||||
}
|
}
|
||||||
@@ -19,13 +19,13 @@ export const MessageStream: SlashCommand = {
|
|||||||
run: async (client: Client, interaction: CommandInteraction) => {
|
run: async (client: Client, interaction: CommandInteraction) => {
|
||||||
// verify channel
|
// verify channel
|
||||||
const channel = await client.channels.fetch(interaction.channelId)
|
const channel = await client.channels.fetch(interaction.channelId)
|
||||||
if (!channel || channel.type !== (ChannelType.PrivateThread && ChannelType.PublicThread && ChannelType.GuildText)) return
|
if (!channel || !UserCommand.includes(channel.type)) return
|
||||||
|
|
||||||
// save value to json and write to it
|
// save value to json and write to it
|
||||||
openConfig(`${interaction.user.username}-config.json`, interaction.commandName, interaction.options.get('stream')?.value)
|
openConfig(`${interaction.user.username}-config.json`, interaction.commandName, interaction.options.get('stream')?.value)
|
||||||
|
|
||||||
interaction.reply({
|
interaction.reply({
|
||||||
content: `Message streaming preferences set to: \`${interaction.options.get('stream')?.value}\``,
|
content: `Message streaming is now set to: \`${interaction.options.get('stream')?.value}\``,
|
||||||
ephemeral: true
|
ephemeral: true
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { ChannelType, Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js'
|
import { Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js'
|
||||||
import { openConfig, SlashCommand } from '../utils/index.js'
|
import { openConfig, SlashCommand, UserCommand } from '../utils/index.js'
|
||||||
|
|
||||||
export const MessageStyle: SlashCommand = {
|
export const MessageStyle: SlashCommand = {
|
||||||
name: 'message-style',
|
name: 'message-style',
|
||||||
@@ -19,7 +19,7 @@ export const MessageStyle: SlashCommand = {
|
|||||||
run: async (client: Client, interaction: CommandInteraction) => {
|
run: async (client: Client, interaction: CommandInteraction) => {
|
||||||
// fetch channel and message
|
// fetch channel and message
|
||||||
const channel = await client.channels.fetch(interaction.channelId)
|
const channel = await client.channels.fetch(interaction.channelId)
|
||||||
if (!channel || channel.type !== (ChannelType.PrivateThread && ChannelType.PublicThread && ChannelType.GuildText)) return
|
if (!channel || !UserCommand.includes(channel.type)) return
|
||||||
|
|
||||||
// set the message style
|
// set the message style
|
||||||
openConfig(`${interaction.user.username}-config.json`, interaction.commandName, interaction.options.get('embed')?.value)
|
openConfig(`${interaction.user.username}-config.json`, interaction.commandName, interaction.options.get('embed')?.value)
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
import { ApplicationCommandOptionType, ChannelType, Client, CommandInteraction } from "discord.js";
|
import { ApplicationCommandOptionType, Client, CommandInteraction } from "discord.js";
|
||||||
import { SlashCommand } from "../utils/commands.js";
|
import { SlashCommand } from "../utils/commands.js";
|
||||||
import { ollama } from "../client.js";
|
import { ollama } from "../client.js";
|
||||||
|
import { ModelResponse } from "ollama";
|
||||||
|
import { UserCommand } from "../utils/index.js";
|
||||||
|
|
||||||
export const PullModel: SlashCommand = {
|
export const PullModel: SlashCommand = {
|
||||||
name: 'pull-model',
|
name: 'pull-model',
|
||||||
description: 'pulls a model from the ollama model library',
|
description: 'pulls a model from the ollama model library. Administrator Only',
|
||||||
|
|
||||||
// set available user options to pass to the command
|
// set available user options to pass to the command
|
||||||
options: [
|
options: [
|
||||||
@@ -24,24 +26,45 @@ export const PullModel: SlashCommand = {
|
|||||||
|
|
||||||
// fetch channel and message
|
// fetch channel and message
|
||||||
const channel = await client.channels.fetch(interaction.channelId)
|
const channel = await client.channels.fetch(interaction.channelId)
|
||||||
if (!channel || channel.type !== (ChannelType.PrivateThread && ChannelType.PublicThread && ChannelType.GuildText)) return
|
if (!channel || !UserCommand.includes(channel.type)) return
|
||||||
|
|
||||||
try {
|
// Admin Command
|
||||||
// call ollama to pull desired model
|
if (!interaction.memberPermissions?.has('Administrator')) {
|
||||||
await ollama.pull({
|
interaction.reply({
|
||||||
model: modelInput
|
content: `${interaction.commandName} is an admin command.\n\nPlease contact a server admin to pull the model you want.`,
|
||||||
})
|
ephemeral: true
|
||||||
} catch (error) {
|
|
||||||
// could not resolve pull or model unfound
|
|
||||||
interaction.editReply({
|
|
||||||
content: `Could not pull/locate the **${modelInput}** model within the [Ollama Model Library](https://ollama.com/library).\n\nPlease check the model library and try again.`
|
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// successful pull
|
// check if model was already pulled
|
||||||
interaction.editReply({
|
const modelExists: boolean = await ollama.list()
|
||||||
content: `Successfully added **${modelInput}** into your local model library.`
|
.then(response => response.models.some((model: ModelResponse) => model.name.startsWith(modelInput)))
|
||||||
})
|
|
||||||
|
try {
|
||||||
|
// call ollama to pull desired model
|
||||||
|
if (!modelExists) {
|
||||||
|
interaction.editReply({
|
||||||
|
content: `**${modelInput}** could not be found. Please wait patiently as I try to retrieve it...`
|
||||||
|
})
|
||||||
|
await ollama.pull({ model: modelInput })
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
// could not resolve pull or model unfound
|
||||||
|
interaction.editReply({
|
||||||
|
content: `Could not retrieve the **${modelInput}** model. You can find models at [Ollama Model Library](https://ollama.com/library).\n\nPlease check the model library and try again.`
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// successful interaction
|
||||||
|
if (modelExists)
|
||||||
|
interaction.editReply({
|
||||||
|
content: `**${modelInput}** is already available.`
|
||||||
|
})
|
||||||
|
else
|
||||||
|
interaction.editReply({
|
||||||
|
content: `Successfully added **${modelInput}**.`
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,28 +1,19 @@
|
|||||||
import { ChannelType, Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js'
|
import { Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js'
|
||||||
import { SlashCommand } from '../utils/commands.js'
|
import { SlashCommand } from '../utils/commands.js'
|
||||||
|
import { AdminCommand } from '../utils/index.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. Administrator Only.',
|
description: 'shutdown the bot. You will need to manually bring it online again. Administrator Only.',
|
||||||
|
|
||||||
// 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
|
// Query for message information and set the style
|
||||||
run: async (client: Client, interaction: CommandInteraction) => {
|
run: async (client: Client, interaction: CommandInteraction) => {
|
||||||
// fetch channel and message
|
// fetch channel and message
|
||||||
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 || !AdminCommand.includes(channel.type)) return
|
||||||
|
|
||||||
// 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(`[Command: shutoff] User ${interaction.user.tag} attempting to shutdown ${client.user!!.tag}`)
|
||||||
|
|
||||||
// check if admin or false on shutdown
|
// check if admin or false on shutdown
|
||||||
if (!interaction.memberPermissions?.has('Administrator')) {
|
if (!interaction.memberPermissions?.has('Administrator')) {
|
||||||
@@ -31,19 +22,14 @@ export const Shutoff: SlashCommand = {
|
|||||||
ephemeral: true
|
ephemeral: true
|
||||||
})
|
})
|
||||||
return // stop from shutting down
|
return // stop from shutting down
|
||||||
} else if (!interaction.options.get('are-you-sure')?.value) {
|
|
||||||
interaction.reply({
|
|
||||||
content: `**Shutdown Aborted:**\n\n${interaction.user.tag}, You didn't want to shutoff **${client.user?.tag}**.`,
|
|
||||||
ephemeral: true
|
|
||||||
})
|
|
||||||
return // chickened out
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Shutoff cleared, do it
|
// 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 shutting down.`,
|
||||||
ephemeral: true
|
ephemeral: true
|
||||||
})
|
})
|
||||||
|
console.log(`[Command: shutoff] ${client.user?.tag} is shutting down.`)
|
||||||
|
|
||||||
// clean up client instance and stop
|
// clean up client instance and stop
|
||||||
client.destroy()
|
client.destroy()
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
import { ApplicationCommandOptionType, ChannelType, Client, CommandInteraction } from "discord.js";
|
import { ApplicationCommandOptionType, Client, CommandInteraction } from "discord.js";
|
||||||
import { SlashCommand } from "../utils/commands.js";
|
import { SlashCommand } from "../utils/commands.js";
|
||||||
import { ollama } from "../client.js";
|
import { ollama } from "../client.js";
|
||||||
import { ModelResponse } from "ollama";
|
import { ModelResponse } from "ollama";
|
||||||
import { openConfig } from "../utils/index.js";
|
import { openConfig, UserCommand } from "../utils/index.js";
|
||||||
|
|
||||||
export const SwitchModel: SlashCommand = {
|
export const SwitchModel: SlashCommand = {
|
||||||
name: 'switch-model',
|
name: 'switch-model',
|
||||||
description: 'switches current model to preferred model to use.',
|
description: 'switches current model to use.',
|
||||||
|
|
||||||
// set available user options to pass to the command
|
// set available user options to pass to the command
|
||||||
options: [
|
options: [
|
||||||
@@ -26,10 +26,10 @@ export const SwitchModel: SlashCommand = {
|
|||||||
|
|
||||||
// fetch channel and message
|
// fetch channel and message
|
||||||
const channel = await client.channels.fetch(interaction.channelId)
|
const channel = await client.channels.fetch(interaction.channelId)
|
||||||
if (!channel || channel.type !== (ChannelType.PrivateThread && ChannelType.PublicThread && ChannelType.GuildText)) return
|
if (!channel || !UserCommand.includes(channel.type)) return
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Phase 1: Set the model
|
// Phase 1: Switch to the model
|
||||||
let switchSuccess = false
|
let switchSuccess = false
|
||||||
await ollama.list()
|
await ollama.list()
|
||||||
.then(response => {
|
.then(response => {
|
||||||
@@ -46,28 +46,21 @@ export const SwitchModel: SlashCommand = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
if (switchSuccess) return
|
// todo: problem can be here if async messes up
|
||||||
|
if (switchSuccess) {
|
||||||
|
// set model now that it exists
|
||||||
|
openConfig(`${interaction.user.username}-config.json`, interaction.commandName, modelInput)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Phase 2: Try to get it regardless
|
// Phase 2: Notify user of failure to find model.
|
||||||
interaction.editReply({
|
interaction.editReply({
|
||||||
content: `Could not find **${modelInput}** in local model library, trying to pull it now...\n\nThis could take a few moments... Please be patient!`
|
content: `Could not find **${modelInput}** in local model library.\n\nPlease contact an server admin for access to this model.`
|
||||||
})
|
|
||||||
|
|
||||||
await ollama.pull({
|
|
||||||
model: modelInput
|
|
||||||
})
|
|
||||||
|
|
||||||
// set model now that it exists
|
|
||||||
openConfig(`${interaction.user.username}-config.json`, interaction.commandName, modelInput)
|
|
||||||
|
|
||||||
// We got the model!
|
|
||||||
interaction.editReply({
|
|
||||||
content: `Successfully added and set **${modelInput}** as your preferred model.`
|
|
||||||
})
|
})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// could not resolve user model switch
|
// could not resolve user model switch
|
||||||
interaction.editReply({
|
interaction.editReply({
|
||||||
content: `Unable to switch user preferred model to **${modelInput}**.\n\n${error}\n\nPossible solution is to run \`/pull-model ${modelInput}\` and try again.`
|
content: `Unable to switch user preferred model to **${modelInput}**.\n\n${error}\n\nPossible solution is to request an server admin run \`/pull-model ${modelInput}\` and try again.`
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { ChannelType, Client, CommandInteraction, TextChannel, ThreadChannel } from 'discord.js'
|
import { ChannelType, Client, CommandInteraction, TextChannel, ThreadChannel } from 'discord.js'
|
||||||
import { openChannelInfo, SlashCommand } from '../utils/index.js'
|
import { AdminCommand, openChannelInfo, SlashCommand } from '../utils/index.js'
|
||||||
|
|
||||||
export const ThreadCreate: SlashCommand = {
|
export const ThreadCreate: SlashCommand = {
|
||||||
name: 'thread',
|
name: 'thread',
|
||||||
@@ -9,7 +9,7 @@ export const ThreadCreate: SlashCommand = {
|
|||||||
run: async (client: Client, interaction: CommandInteraction) => {
|
run: async (client: Client, interaction: CommandInteraction) => {
|
||||||
// fetch the channel
|
// fetch the channel
|
||||||
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 || !AdminCommand.includes(channel.type)) return
|
||||||
|
|
||||||
const thread = await (channel as TextChannel).threads.create({
|
const thread = await (channel as TextChannel).threads.create({
|
||||||
name: `${client.user?.username}-support-${Date.now()}`,
|
name: `${client.user?.username}-support-${Date.now()}`,
|
||||||
@@ -18,7 +18,7 @@ export const ThreadCreate: SlashCommand = {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// Send a message in the thread
|
// Send a message in the thread
|
||||||
thread.send(`Hello ${interaction.user} and others! \n\nIt's nice to meet you. Please talk to me by typing **@${client.user?.username}** with your prompt.`)
|
thread.send(`Hello ${interaction.user} and others! \n\nIt's nice to meet you. Please talk to me by typing **@${client.user?.username}** with your message.`)
|
||||||
|
|
||||||
// handle storing this chat channel
|
// handle storing this chat channel
|
||||||
openChannelInfo(thread.id,
|
openChannelInfo(thread.id,
|
||||||
@@ -27,7 +27,7 @@ export const ThreadCreate: SlashCommand = {
|
|||||||
|
|
||||||
// user only reply
|
// user only reply
|
||||||
return interaction.reply({
|
return interaction.reply({
|
||||||
content: `I can help you in thread **${thread.id}** below.`,
|
content: `I can help you in <#${thread.id}> below.`,
|
||||||
ephemeral: true
|
ephemeral: true
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { ChannelType, Client, CommandInteraction, TextChannel, ThreadChannel } from 'discord.js'
|
import { ChannelType, Client, CommandInteraction, TextChannel, ThreadChannel } from 'discord.js'
|
||||||
import { openChannelInfo, SlashCommand } from '../utils/index.js'
|
import { AdminCommand, openChannelInfo, SlashCommand } from '../utils/index.js'
|
||||||
|
|
||||||
export const PrivateThreadCreate: SlashCommand = {
|
export const PrivateThreadCreate: SlashCommand = {
|
||||||
name: 'private-thread',
|
name: 'private-thread',
|
||||||
@@ -9,7 +9,7 @@ export const PrivateThreadCreate: SlashCommand = {
|
|||||||
run: async (client: Client, interaction: CommandInteraction) => {
|
run: async (client: Client, interaction: CommandInteraction) => {
|
||||||
// fetch the channel
|
// fetch the channel
|
||||||
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 || !AdminCommand.includes(channel.type)) return
|
||||||
|
|
||||||
const thread = await (channel as TextChannel).threads.create({
|
const thread = await (channel as TextChannel).threads.create({
|
||||||
name: `${client.user?.username}-private-support-${Date.now()}`,
|
name: `${client.user?.username}-private-support-${Date.now()}`,
|
||||||
@@ -29,7 +29,7 @@ export const PrivateThreadCreate: SlashCommand = {
|
|||||||
|
|
||||||
// user only reply
|
// user only reply
|
||||||
return interaction.reply({
|
return interaction.reply({
|
||||||
content: `I can help you in thread **${thread.id}**. Please refer to the private channel below this one.`,
|
content: `I can help you in <#${thread.id}>.`,
|
||||||
ephemeral: true
|
ephemeral: true
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import { getChannelInfo, getServerConfig, getUserConfig, openChannelInfo, openCo
|
|||||||
*
|
*
|
||||||
* @param message the message received from the channel
|
* @param message the message received from the channel
|
||||||
*/
|
*/
|
||||||
export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client }, message) => {
|
export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client, defaultModel }, message) => {
|
||||||
const clientId = client.user!!.id
|
const clientId = client.user!!.id
|
||||||
const cleanedMessage = clean(message.content, clientId)
|
const cleanedMessage = clean(message.content, clientId)
|
||||||
log(`Message \"${cleanedMessage}\" from ${message.author.tag} in channel/thread ${message.channelId}.`)
|
log(`Message \"${cleanedMessage}\" from ${message.author.tag} in channel/thread ${message.channelId}.`)
|
||||||
@@ -22,56 +22,87 @@ export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client
|
|||||||
// default stream to false
|
// default stream to false
|
||||||
let shouldStream = false
|
let shouldStream = false
|
||||||
|
|
||||||
|
// Params for Preferences Fetching
|
||||||
|
const maxRetries = 3
|
||||||
|
const delay = 1000 // in millisecons
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Retrieve Server/Guild Preferences
|
// Retrieve Server/Guild Preferences
|
||||||
await new Promise((resolve, reject) => {
|
let attempt = 0
|
||||||
getServerConfig(`${message.guildId}-config.json`, (config) => {
|
while (attempt < maxRetries) {
|
||||||
// check if config.json exists
|
try {
|
||||||
if (config === undefined) {
|
await new Promise((resolve, reject) => {
|
||||||
// Allowing chat options to be available
|
getServerConfig(`${message.guildId}-config.json`, (config) => {
|
||||||
openConfig(`${message.guildId}-config.json`, 'toggle-chat', true)
|
// check if config.json exists
|
||||||
reject(new Error('No Server Preferences is set up.\n\nCreating default server preferences file...\nPlease try chatting again.'))
|
if (config === undefined) {
|
||||||
return
|
// Allowing chat options to be available
|
||||||
}
|
openConfig(`${message.guildId}-config.json`, 'toggle-chat', true)
|
||||||
|
reject(new Error('Failed to locate or create Server Preferences\n\nPlease try chatting again...'))
|
||||||
|
}
|
||||||
|
|
||||||
// check if chat is disabled
|
// check if chat is disabled
|
||||||
if (!config.options['toggle-chat']) {
|
else if (!config.options['toggle-chat'])
|
||||||
reject(new Error('Admin(s) have disabled chat features.\n\n Please contact your server\'s admin(s).'))
|
reject(new Error('Admin(s) have disabled chat features.\n\n Please contact your server\'s admin(s).'))
|
||||||
return
|
else
|
||||||
}
|
resolve(config)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
break // successful
|
||||||
|
} catch (error) {
|
||||||
|
++attempt
|
||||||
|
if (attempt < maxRetries) {
|
||||||
|
log(`Attempt ${attempt} failed for Server Preferences. Retrying in ${delay}ms...`)
|
||||||
|
await new Promise(ret => setTimeout(ret, delay))
|
||||||
|
} else
|
||||||
|
throw new Error(`Could not retrieve Server Preferences, please try chatting again...`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
resolve(config)
|
// Reset attempts for User preferences
|
||||||
})
|
attempt = 0
|
||||||
})
|
let userConfig: UserConfig | undefined
|
||||||
|
|
||||||
// Retrieve User Preferences
|
while (attempt < maxRetries) {
|
||||||
const userConfig: UserConfig = await new Promise((resolve, reject) => {
|
try {
|
||||||
getUserConfig(`${message.author.username}-config.json`, (config) => {
|
// Retrieve User Preferences
|
||||||
if (config === undefined) {
|
userConfig = await new Promise((resolve, reject) => {
|
||||||
openConfig(`${message.author.username}-config.json`, 'message-style', false)
|
getUserConfig(`${message.author.username}-config.json`, (config) => {
|
||||||
reject(new Error('No User Preferences is set up.\n\nCreating preferences file with \`message-style\` set as \`false\` for regular message style.\nPlease try chatting again.'))
|
if (config === undefined) {
|
||||||
return
|
openConfig(`${message.author.username}-config.json`, 'message-style', false)
|
||||||
}
|
openConfig(`${message.author.username}-config.json`, 'switch-model', defaultModel)
|
||||||
|
reject(new Error('No User Preferences is set up.\n\nCreating preferences file with \`message-style\` set as \`false\` for regular message style.\nPlease try chatting again.'))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// check if there is a set capacity in config
|
// check if there is a set capacity in config
|
||||||
if (typeof config.options['modify-capacity'] !== 'number')
|
else if (typeof config.options['modify-capacity'] !== 'number')
|
||||||
log(`Capacity is undefined, using default capacity of ${msgHist.capacity}.`)
|
log(`Capacity is undefined, using default capacity of ${msgHist.capacity}.`)
|
||||||
else if (config.options['modify-capacity'] === msgHist.capacity)
|
else if (config.options['modify-capacity'] === msgHist.capacity)
|
||||||
log(`Capacity matches config as ${msgHist.capacity}, no changes made.`)
|
log(`Capacity matches config as ${msgHist.capacity}, no changes made.`)
|
||||||
else {
|
else {
|
||||||
log(`New Capacity found. Setting Context Capacity to ${config.options['modify-capacity']}.`)
|
log(`New Capacity found. Setting Context Capacity to ${config.options['modify-capacity']}.`)
|
||||||
msgHist.capacity = config.options['modify-capacity']
|
msgHist.capacity = config.options['modify-capacity']
|
||||||
}
|
}
|
||||||
|
|
||||||
// set stream state
|
// set stream state
|
||||||
shouldStream = config.options['message-stream'] as boolean || false
|
shouldStream = config.options['message-stream'] as boolean || false
|
||||||
|
|
||||||
if (typeof config.options['switch-model'] !== 'string')
|
if (typeof config.options['switch-model'] !== 'string')
|
||||||
reject(new Error(`No Model was set. Please set a model by running \`/switch-model <model of choice>\`.\n\nIf you do not have any models. Run \`/pull-model <model name>\`.`))
|
reject(new Error(`No Model was set. Please set a model by running \`/switch-model <model of choice>\`.\n\nIf you do not have any models. Run \`/pull-model <model name>\`.`))
|
||||||
|
|
||||||
resolve(config)
|
resolve(config)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
break // successful
|
||||||
|
} catch (error) {
|
||||||
|
++attempt
|
||||||
|
if (attempt < maxRetries) {
|
||||||
|
log(`Attempt ${attempt} failed for User Preferences. Retrying in ${delay}ms...`)
|
||||||
|
await new Promise(ret => setTimeout(ret, delay))
|
||||||
|
} else
|
||||||
|
throw new Error(`Could not retrieve User Preferences, please try chatting again...`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// need new check for "open/active" threads/channels here!
|
// need new check for "open/active" threads/channels here!
|
||||||
let chatMessages: UserMessage[] = await new Promise((resolve) => {
|
let chatMessages: UserMessage[] = await new Promise((resolve) => {
|
||||||
@@ -106,6 +137,9 @@ export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client
|
|||||||
// response string for ollama to put its response
|
// response string for ollama to put its response
|
||||||
let response: string
|
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.`)
|
||||||
|
|
||||||
// get message attachment if exists
|
// get message attachment if exists
|
||||||
const messageAttachment: string[] = await getAttachmentData(message.attachments.first())
|
const messageAttachment: string[] = await getAttachmentData(message.attachments.first())
|
||||||
const model: string = userConfig.options['switch-model']
|
const model: string = userConfig.options['switch-model']
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ export const Keys = {
|
|||||||
clientToken: getEnvVar('CLIENT_TOKEN'),
|
clientToken: getEnvVar('CLIENT_TOKEN'),
|
||||||
ipAddress: getEnvVar('OLLAMA_IP', '127.0.0.1'), // default ollama ip if none
|
ipAddress: getEnvVar('OLLAMA_IP', '127.0.0.1'), // default ollama ip if none
|
||||||
portAddress: getEnvVar('OLLAMA_PORT', '11434'), // default ollama port if none
|
portAddress: getEnvVar('OLLAMA_PORT', '11434'), // default ollama port if none
|
||||||
|
defaultModel: getEnvVar('MODEL', 'llama3.2')
|
||||||
} as const // readonly keys
|
} as const // readonly keys
|
||||||
|
|
||||||
export default Keys
|
export default Keys
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { ChannelType } from 'discord.js'
|
||||||
import { UserMessage } from './index.js'
|
import { UserMessage } from './index.js'
|
||||||
|
|
||||||
export interface UserConfiguration {
|
export interface UserConfiguration {
|
||||||
@@ -42,6 +43,21 @@ export interface Channel {
|
|||||||
messages: UserMessage[]
|
messages: UserMessage[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The following 2 types is allow for better readability in commands
|
||||||
|
* Admin Command -> Don't run in Threads
|
||||||
|
* User Command -> Used anywhere
|
||||||
|
*/
|
||||||
|
export const AdminCommand = [
|
||||||
|
ChannelType.GuildText
|
||||||
|
]
|
||||||
|
|
||||||
|
export const UserCommand = [
|
||||||
|
ChannelType.GuildText,
|
||||||
|
ChannelType.PublicThread,
|
||||||
|
ChannelType.PrivateThread
|
||||||
|
]
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if the configuration we are editing/taking from is a Server Config
|
* Check if the configuration we are editing/taking from is a Server Config
|
||||||
* @param key name of command we ran
|
* @param key name of command we ran
|
||||||
|
|||||||
@@ -36,7 +36,8 @@ export interface EventProps {
|
|||||||
client: Client
|
client: Client
|
||||||
log: LogMethod
|
log: LogMethod
|
||||||
msgHist: Queue<UserMessage>
|
msgHist: Queue<UserMessage>
|
||||||
ollama: Ollama
|
ollama: Ollama,
|
||||||
|
defaultModel: String
|
||||||
}
|
}
|
||||||
export type EventCallback<T extends EventKeys> = (
|
export type EventCallback<T extends EventKeys> = (
|
||||||
props: EventProps,
|
props: EventProps,
|
||||||
@@ -64,7 +65,8 @@ export function registerEvents(
|
|||||||
client: Client,
|
client: Client,
|
||||||
events: Event[],
|
events: Event[],
|
||||||
msgHist: Queue<UserMessage>,
|
msgHist: Queue<UserMessage>,
|
||||||
ollama: Ollama
|
ollama: Ollama,
|
||||||
|
defaultModel: String
|
||||||
): void {
|
): void {
|
||||||
for (const { key, callback } of events) {
|
for (const { key, callback } of events) {
|
||||||
client.on(key, (...args) => {
|
client.on(key, (...args) => {
|
||||||
@@ -73,7 +75,7 @@ export function registerEvents(
|
|||||||
|
|
||||||
// Handle Errors, call callback, log errors as needed
|
// Handle Errors, call callback, log errors as needed
|
||||||
try {
|
try {
|
||||||
callback({ client, log, msgHist, ollama }, ...args)
|
callback({ client, log, msgHist, ollama, defaultModel }, ...args)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log('[Uncaught Error]', error)
|
log('[Uncaught Error]', error)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,7 +53,6 @@ export async function clearChannelInfo(filename: string, channel: TextChannel, u
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
console.log(cleanedHistory)
|
|
||||||
return cleanedHistory
|
return cleanedHistory
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -73,7 +73,10 @@ export async function normalMessage(
|
|||||||
}
|
}
|
||||||
} catch(error: any) {
|
} catch(error: any) {
|
||||||
console.log(`[Util: messageNormal] Error creating message: ${error.message}`)
|
console.log(`[Util: messageNormal] Error creating message: ${error.message}`)
|
||||||
sentMessage.edit(`**Response generation failed.**\n\nReason: ${error.message}`)
|
if (error.message.includes('try pulling it first'))
|
||||||
|
sentMessage.edit(`**Response generation failed.**\n\nReason: You do not have the ${model} downloaded. Ask an admin to pull it using the \`pull-model\` command.`)
|
||||||
|
else
|
||||||
|
sentMessage.edit(`**Response generation failed.**\n\nReason: ${error.message}`)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user