mirror of
https://github.com/kevinthedang/discord-ollama.git
synced 2025-12-12 11:56:06 -05:00
Pull/Switch Model Commands Fix (#137)
* Update: Channel checker and channel name gone * Add: note of where problem can be * Update: Check if model already exists for Pull Command * Add: User/Admin Command Constants * Update: version increment
This commit is contained in:
@@ -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.1
|
||||
image: kevinthedang/discord-ollama:0.7.2
|
||||
environment:
|
||||
CLIENT_TOKEN: ${CLIENT_TOKEN}
|
||||
OLLAMA_IP: ${OLLAMA_IP}
|
||||
|
||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "discord-ollama",
|
||||
"version": "0.7.1",
|
||||
"version": "0.7.2",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "discord-ollama",
|
||||
"version": "0.7.1",
|
||||
"version": "0.7.2",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"discord.js": "^14.16.3",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "discord-ollama",
|
||||
"version": "0.7.1",
|
||||
"version": "0.7.2",
|
||||
"description": "Ollama Integration into discord",
|
||||
"main": "build/index.js",
|
||||
"exports": "./build/index.js",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { ChannelType, Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js'
|
||||
import { openConfig, SlashCommand } from '../utils/index.js'
|
||||
import { Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js'
|
||||
import { openConfig, SlashCommand, UserCommand } from '../utils/index.js'
|
||||
|
||||
export const Capacity: SlashCommand = {
|
||||
name: 'modify-capacity',
|
||||
@@ -19,7 +19,7 @@ export const Capacity: SlashCommand = {
|
||||
run: async (client: Client, interaction: CommandInteraction) => {
|
||||
// fetch channel and message
|
||||
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
|
||||
openConfig(`${interaction.user.username}-config.json`, interaction.commandName, interaction.options.get('context-capacity')?.value)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { ChannelType, Client, CommandInteraction, TextChannel } from 'discord.js'
|
||||
import { clearChannelInfo, SlashCommand } from '../utils/index.js'
|
||||
import { Channel, Client, CommandInteraction, TextChannel } from 'discord.js'
|
||||
import { clearChannelInfo, SlashCommand, UserCommand } from '../utils/index.js'
|
||||
|
||||
export const ClearUserChannelHistory: SlashCommand = {
|
||||
name: 'clear-user-channel-history',
|
||||
@@ -8,10 +8,10 @@ export const ClearUserChannelHistory: SlashCommand = {
|
||||
// Clear channel history for intended user
|
||||
run: async (client: Client, interaction: CommandInteraction) => {
|
||||
// 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 (!channel || channel.type !== ChannelType.GuildText) return
|
||||
if (!channel || !UserCommand.includes(channel.type)) return
|
||||
|
||||
// clear channel info for user
|
||||
const successfulWipe = await clearChannelInfo(interaction.channelId,
|
||||
@@ -21,12 +21,12 @@ export const ClearUserChannelHistory: SlashCommand = {
|
||||
// check result of clearing history
|
||||
if (successfulWipe)
|
||||
interaction.reply({
|
||||
content: `Channel history in **${channel.name}** cleared for **${interaction.user.username}**.`,
|
||||
content: `Channel history in **this channel** successfully cleared for **${interaction.user.username}**.`,
|
||||
ephemeral: true
|
||||
})
|
||||
else
|
||||
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: `Channel history could not be found for **${interaction.user.username}** in **this channel**.\n\nPlease chat with **${client.user?.username}** to start a chat history.`,
|
||||
ephemeral: true
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { ChannelType, 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 = {
|
||||
name: 'toggle-chat',
|
||||
@@ -19,7 +19,7 @@ export const Disable: SlashCommand = {
|
||||
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
|
||||
if (!channel || !AdminCommand.includes(channel.type)) return
|
||||
|
||||
// check if runner is an admin
|
||||
if (!interaction.memberPermissions?.has('Administrator')) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { ApplicationCommandOptionType, ChannelType, Client, CommandInteraction } from 'discord.js'
|
||||
import { openConfig, SlashCommand } from '../utils/index.js'
|
||||
import { ApplicationCommandOptionType, Client, CommandInteraction } from 'discord.js'
|
||||
import { openConfig, SlashCommand, UserCommand } from '../utils/index.js'
|
||||
|
||||
export const MessageStream: SlashCommand = {
|
||||
name: 'message-stream',
|
||||
@@ -19,7 +19,7 @@ export const MessageStream: SlashCommand = {
|
||||
run: async (client: Client, interaction: CommandInteraction) => {
|
||||
// verify channel
|
||||
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
|
||||
openConfig(`${interaction.user.username}-config.json`, interaction.commandName, interaction.options.get('stream')?.value)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { ChannelType, Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js'
|
||||
import { openConfig, SlashCommand } from '../utils/index.js'
|
||||
import { Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js'
|
||||
import { openConfig, SlashCommand, UserCommand } from '../utils/index.js'
|
||||
|
||||
export const MessageStyle: SlashCommand = {
|
||||
name: 'message-style',
|
||||
@@ -19,7 +19,7 @@ export const MessageStyle: SlashCommand = {
|
||||
run: async (client: Client, interaction: CommandInteraction) => {
|
||||
// fetch channel and message
|
||||
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
|
||||
openConfig(`${interaction.user.username}-config.json`, interaction.commandName, interaction.options.get('embed')?.value)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { ApplicationCommandOptionType, ChannelType, Client, CommandInteraction } from "discord.js";
|
||||
import { ApplicationCommandOptionType, Client, CommandInteraction } from "discord.js";
|
||||
import { SlashCommand } from "../utils/commands.js";
|
||||
import { ollama } from "../client.js";
|
||||
import { ModelResponse } from "ollama";
|
||||
import { UserCommand } from "../utils/index.js";
|
||||
|
||||
export const PullModel: SlashCommand = {
|
||||
name: 'pull-model',
|
||||
@@ -24,13 +26,16 @@ export const PullModel: SlashCommand = {
|
||||
|
||||
// fetch channel and message
|
||||
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
|
||||
|
||||
// check if model was already pulled
|
||||
const modelExists: boolean = await ollama.list()
|
||||
.then(response => response.models.some((model: ModelResponse) => model.name.startsWith(modelInput)))
|
||||
|
||||
try {
|
||||
// call ollama to pull desired model
|
||||
await ollama.pull({
|
||||
model: modelInput
|
||||
})
|
||||
if (!modelExists)
|
||||
await ollama.pull({ model: modelInput })
|
||||
} catch (error) {
|
||||
// could not resolve pull or model unfound
|
||||
interaction.editReply({
|
||||
@@ -39,9 +44,14 @@ export const PullModel: SlashCommand = {
|
||||
return
|
||||
}
|
||||
|
||||
// successful pull
|
||||
interaction.editReply({
|
||||
content: `Successfully added **${modelInput}** into your local model library.`
|
||||
})
|
||||
// successful interaction
|
||||
if (modelExists)
|
||||
interaction.editReply({
|
||||
content: `**${modelInput}** is already in your local model library.`
|
||||
})
|
||||
else
|
||||
interaction.editReply({
|
||||
content: `Successfully added **${modelInput}** into your local model library.`
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { ChannelType, Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js'
|
||||
import { Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js'
|
||||
import { SlashCommand } from '../utils/commands.js'
|
||||
import { AdminCommand } from '../utils/index.js'
|
||||
|
||||
export const Shutoff: SlashCommand = {
|
||||
name: 'shutoff',
|
||||
@@ -19,7 +20,7 @@ export const Shutoff: SlashCommand = {
|
||||
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
|
||||
if (!channel || !AdminCommand.includes(channel.type)) 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}`)
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { ApplicationCommandOptionType, ChannelType, Client, CommandInteraction } from "discord.js";
|
||||
import { ApplicationCommandOptionType, Client, CommandInteraction } from "discord.js";
|
||||
import { SlashCommand } from "../utils/commands.js";
|
||||
import { ollama } from "../client.js";
|
||||
import { ModelResponse } from "ollama";
|
||||
import { openConfig } from "../utils/index.js";
|
||||
import { openConfig, UserCommand } from "../utils/index.js";
|
||||
|
||||
export const SwitchModel: SlashCommand = {
|
||||
name: 'switch-model',
|
||||
@@ -26,7 +26,7 @@ export const SwitchModel: SlashCommand = {
|
||||
|
||||
// fetch channel and message
|
||||
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 {
|
||||
// Phase 1: Set the model
|
||||
@@ -46,6 +46,7 @@ export const SwitchModel: SlashCommand = {
|
||||
}
|
||||
}
|
||||
})
|
||||
// todo: problem can be here if async messes up
|
||||
if (switchSuccess) return
|
||||
|
||||
// Phase 2: Try to get it regardless
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
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 = {
|
||||
name: 'thread',
|
||||
@@ -9,7 +9,7 @@ export const ThreadCreate: SlashCommand = {
|
||||
run: async (client: Client, interaction: CommandInteraction) => {
|
||||
// fetch the channel
|
||||
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({
|
||||
name: `${client.user?.username}-support-${Date.now()}`,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
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 = {
|
||||
name: 'private-thread',
|
||||
@@ -9,7 +9,7 @@ export const PrivateThreadCreate: SlashCommand = {
|
||||
run: async (client: Client, interaction: CommandInteraction) => {
|
||||
// fetch the channel
|
||||
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({
|
||||
name: `${client.user?.username}-private-support-${Date.now()}`,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { ChannelType } from 'discord.js'
|
||||
import { UserMessage } from './index.js'
|
||||
|
||||
export interface UserConfiguration {
|
||||
@@ -42,6 +43,21 @@ export interface Channel {
|
||||
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
|
||||
* @param key name of command we ran
|
||||
|
||||
@@ -53,7 +53,6 @@ export async function clearChannelInfo(filename: string, channel: TextChannel, u
|
||||
}
|
||||
})
|
||||
})
|
||||
console.log(cleanedHistory)
|
||||
return cleanedHistory
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user