Skip to content

Commit 4078381

Browse files
Upgrade Npm Packages (#159)
* Update: upgrade packages * Update: add in all packages * Update: fix whitespace in events --------- Co-authored-by: JT2M0L3Y <[email protected]>
1 parent ed0d860 commit 4078381

7 files changed

+745
-676
lines changed

package-lock.json

+546-473
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+8-8
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,22 @@
2727
"author": "Kevin Dang",
2828
"license": "ISC",
2929
"dependencies": {
30-
"discord.js": "^14.17.3",
30+
"discord.js": "^14.18.0",
3131
"dotenv": "^16.4.7",
32-
"ollama": "^0.5.11",
32+
"ollama": "^0.5.13",
3333
"redis": "^4.7.0"
3434
},
3535
"devDependencies": {
36-
"@types/node": "^22.10.2",
37-
"@vitest/coverage-v8": "^2.1.8",
36+
"@types/node": "^22.13.5",
37+
"@vitest/coverage-v8": "^3.0.6",
3838
"ts-node": "^10.9.2",
39-
"tsx": "^4.19.2",
40-
"typescript": "^5.7.2",
41-
"vitest": "^2.1.4"
39+
"tsx": "^4.19.3",
40+
"typescript": "^5.7.3",
41+
"vitest": "^3.0.4"
4242
},
4343
"type": "module",
4444
"engines": {
4545
"npm": ">=10.9.0",
4646
"node": ">=22.12.0"
4747
}
48-
}
48+
}

src/events/interactionCreate.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ import commands from '../commands/index.js'
66
* @param interaction the interaction received from the server
77
*/
88
export default event(Events.InteractionCreate, async ({ log, client }, interaction) => {
9-
if (!interaction.isCommand() || !interaction.isChatInputCommand()) return
9+
if (!interaction.isCommand() || !interaction.isChatInputCommand()) return
1010

11-
log(`Interaction called \'${interaction.commandName}\' from ${interaction.user.tag}.`)
11+
log(`Interaction called \'${interaction.commandName}\' from ${interaction.user.tag}.`)
1212

13-
// ensure command exists, otherwise kill event
14-
const command = commands.find(command => command.name === interaction.commandName)
15-
if (!command) return
13+
// ensure command exists, otherwise kill event
14+
const command = commands.find(command => command.name === interaction.commandName)
15+
if (!command) return
1616

17-
// the command exists, execute it
18-
command.run(client, interaction)
19-
}
20-
)
17+
// the command exists, execute it
18+
command.run(client, interaction)
19+
})

src/events/messageCreate.ts

+147-148
Original file line numberDiff line numberDiff line change
@@ -16,129 +16,129 @@ export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client
1616
let cleanedMessage = clean(message.content, clientId)
1717
log(`Message \"${cleanedMessage}\" from ${message.author.tag} in channel/thread ${message.channelId}.`)
1818

19-
// Do not respond if bot talks in the chat
20-
if (message.author.username === message.client.user.username) return
21-
22-
// Only respond if message mentions the bot
23-
if (!message.mentions.has(clientId)) return
24-
25-
// default stream to false
26-
let shouldStream = false
27-
28-
// Params for Preferences Fetching
29-
const maxRetries = 3
30-
const delay = 1000 // in millisecons
31-
32-
try {
33-
// Retrieve Server/Guild Preferences
34-
let attempt = 0
35-
while (attempt < maxRetries) {
36-
try {
37-
await new Promise((resolve, reject) => {
38-
getServerConfig(`${message.guildId}-config.json`, (config) => {
39-
// check if config.json exists
40-
if (config === undefined) {
41-
// Allowing chat options to be available
42-
openConfig(`${message.guildId}-config.json`, 'toggle-chat', true)
43-
reject(new Error('Failed to locate or create Server Preferences\n\nPlease try chatting again...'))
44-
}
45-
46-
// check if chat is disabled
47-
else if (!config.options['toggle-chat'])
48-
reject(new Error('Admin(s) have disabled chat features.\n\n Please contact your server\'s admin(s).'))
49-
else
50-
resolve(config)
51-
})
19+
// Do not respond if bot talks in the chat
20+
if (message.author.username === message.client.user.username) return
21+
22+
// Only respond if message mentions the bot
23+
if (!message.mentions.has(clientId)) return
24+
25+
// default stream to false
26+
let shouldStream = false
27+
28+
// Params for Preferences Fetching
29+
const maxRetries = 3
30+
const delay = 1000 // in millisecons
31+
32+
try {
33+
// Retrieve Server/Guild Preferences
34+
let attempt = 0
35+
while (attempt < maxRetries) {
36+
try {
37+
await new Promise((resolve, reject) => {
38+
getServerConfig(`${message.guildId}-config.json`, (config) => {
39+
// check if config.json exists
40+
if (config === undefined) {
41+
// Allowing chat options to be available
42+
openConfig(`${message.guildId}-config.json`, 'toggle-chat', true)
43+
reject(new Error('Failed to locate or create Server Preferences\n\nPlease try chatting again...'))
44+
}
45+
46+
// check if chat is disabled
47+
else if (!config.options['toggle-chat'])
48+
reject(new Error('Admin(s) have disabled chat features.\n\n Please contact your server\'s admin(s).'))
49+
else
50+
resolve(config)
5251
})
53-
break // successful
54-
} catch (error) {
55-
++attempt
56-
if (attempt < maxRetries) {
57-
log(`Attempt ${attempt} failed for Server Preferences. Retrying in ${delay}ms...`)
58-
await new Promise(ret => setTimeout(ret, delay))
59-
} else
60-
throw new Error(`Could not retrieve Server Preferences, please try chatting again...`)
61-
}
52+
})
53+
break // successful
54+
} catch (error) {
55+
++attempt
56+
if (attempt < maxRetries) {
57+
log(`Attempt ${attempt} failed for Server Preferences. Retrying in ${delay}ms...`)
58+
await new Promise(ret => setTimeout(ret, delay))
59+
} else
60+
throw new Error(`Could not retrieve Server Preferences, please try chatting again...`)
6261
}
62+
}
6363

64-
// Reset attempts for User preferences
65-
attempt = 0
66-
let userConfig: UserConfig | undefined
67-
68-
while (attempt < maxRetries) {
69-
try {
70-
// Retrieve User Preferences
71-
userConfig = await new Promise((resolve, reject) => {
72-
getUserConfig(`${message.author.username}-config.json`, (config) => {
73-
if (config === undefined) {
74-
openConfig(`${message.author.username}-config.json`, 'message-style', false)
75-
openConfig(`${message.author.username}-config.json`, 'switch-model', defaultModel)
76-
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.'))
77-
return
78-
}
79-
80-
// check if there is a set capacity in config
81-
else if (typeof config.options['modify-capacity'] !== 'number')
82-
log(`Capacity is undefined, using default capacity of ${msgHist.capacity}.`)
83-
else if (config.options['modify-capacity'] === msgHist.capacity)
84-
log(`Capacity matches config as ${msgHist.capacity}, no changes made.`)
85-
else {
86-
log(`New Capacity found. Setting Context Capacity to ${config.options['modify-capacity']}.`)
87-
msgHist.capacity = config.options['modify-capacity']
88-
}
89-
90-
// set stream state
91-
shouldStream = config.options['message-stream'] as boolean || false
92-
93-
if (typeof config.options['switch-model'] !== 'string')
94-
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>\`.`))
64+
// Reset attempts for User preferences
65+
attempt = 0
66+
let userConfig: UserConfig | undefined
67+
68+
while (attempt < maxRetries) {
69+
try {
70+
// Retrieve User Preferences
71+
userConfig = await new Promise((resolve, reject) => {
72+
getUserConfig(`${message.author.username}-config.json`, (config) => {
73+
if (config === undefined) {
74+
openConfig(`${message.author.username}-config.json`, 'message-style', false)
75+
openConfig(`${message.author.username}-config.json`, 'switch-model', defaultModel)
76+
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.'))
77+
return
78+
}
9579

96-
resolve(config)
97-
})
80+
// check if there is a set capacity in config
81+
else if (typeof config.options['modify-capacity'] !== 'number')
82+
log(`Capacity is undefined, using default capacity of ${msgHist.capacity}.`)
83+
else if (config.options['modify-capacity'] === msgHist.capacity)
84+
log(`Capacity matches config as ${msgHist.capacity}, no changes made.`)
85+
else {
86+
log(`New Capacity found. Setting Context Capacity to ${config.options['modify-capacity']}.`)
87+
msgHist.capacity = config.options['modify-capacity']
88+
}
89+
90+
// set stream state
91+
shouldStream = config.options['message-stream'] as boolean || false
92+
93+
if (typeof config.options['switch-model'] !== 'string')
94+
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>\`.`))
95+
96+
resolve(config)
9897
})
99-
break // successful
100-
} catch (error) {
101-
++attempt
102-
if (attempt < maxRetries) {
103-
log(`Attempt ${attempt} failed for User Preferences. Retrying in ${delay}ms...`)
104-
await new Promise(ret => setTimeout(ret, delay))
105-
} else
106-
throw new Error(`Could not retrieve User Preferences, please try chatting again...`)
107-
}
98+
})
99+
break // successful
100+
} catch (error) {
101+
++attempt
102+
if (attempt < maxRetries) {
103+
log(`Attempt ${attempt} failed for User Preferences. Retrying in ${delay}ms...`)
104+
await new Promise(ret => setTimeout(ret, delay))
105+
} else
106+
throw new Error(`Could not retrieve User Preferences, please try chatting again...`)
108107
}
108+
}
109109

110-
// need new check for "open/active" threads/channels here!
111-
let chatMessages: UserMessage[] = await new Promise((resolve) => {
112-
// set new queue to modify
110+
// need new check for "open/active" threads/channels here!
111+
let chatMessages: UserMessage[] = await new Promise((resolve) => {
112+
// set new queue to modify
113+
getChannelInfo(`${message.channelId}-${message.author.username}.json`, (channelInfo) => {
114+
if (channelInfo?.messages)
115+
resolve(channelInfo.messages)
116+
else {
117+
log(`Channel/Thread ${message.channel}-${message.author.username} does not exist. File will be created shortly...`)
118+
resolve([])
119+
}
120+
})
121+
})
122+
123+
if (chatMessages.length === 0) {
124+
chatMessages = await new Promise((resolve, reject) => {
125+
openChannelInfo(message.channelId,
126+
message.channel as TextChannel,
127+
message.author.tag
128+
)
113129
getChannelInfo(`${message.channelId}-${message.author.username}.json`, (channelInfo) => {
114130
if (channelInfo?.messages)
115131
resolve(channelInfo.messages)
116132
else {
117133
log(`Channel/Thread ${message.channel}-${message.author.username} does not exist. File will be created shortly...`)
118-
resolve([])
134+
reject(new Error(`Failed to find ${message.author.username}'s history. Try chatting again.`))
119135
}
120136
})
121137
})
138+
}
122139

123-
if (chatMessages.length === 0) {
124-
chatMessages = await new Promise((resolve, reject) => {
125-
openChannelInfo(message.channelId,
126-
message.channel as TextChannel,
127-
message.author.tag
128-
)
129-
getChannelInfo(`${message.channelId}-${message.author.username}.json`, (channelInfo) => {
130-
if (channelInfo?.messages)
131-
resolve(channelInfo.messages)
132-
else {
133-
log(`Channel/Thread ${message.channel}-${message.author.username} does not exist. File will be created shortly...`)
134-
reject(new Error(`Failed to find ${message.author.username}'s history. Try chatting again.`))
135-
}
136-
})
137-
})
138-
}
139-
140-
if (!userConfig)
141-
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.`)
140+
if (!userConfig)
141+
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.`)
142142

143143
// get message attachment if exists
144144
const attachment = message.attachments.first()
@@ -148,47 +148,46 @@ export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client
148148
cleanedMessage += await getTextFileAttachmentData(attachment)
149149
else if (attachment)
150150
messageAttachment = await getAttachmentData(attachment)
151-
152-
const model: string = userConfig.options['switch-model']
153-
154-
// set up new queue
155-
msgHist.setQueue(chatMessages)
156151

157-
// check if we can push, if not, remove oldest
158-
while (msgHist.size() >= msgHist.capacity) msgHist.dequeue()
159-
160-
// push user response before ollama query
161-
msgHist.enqueue({
162-
role: 'user',
163-
content: cleanedMessage,
164-
images: messageAttachment || []
165-
})
166-
167-
// response string for ollama to put its response
168-
const response: string = await normalMessage(message, ollama, model, msgHist, shouldStream)
169-
170-
// If something bad happened, remove user query and stop
171-
if (response == undefined) { msgHist.pop(); return }
172-
173-
// if queue is full, remove the oldest message
174-
while (msgHist.size() >= msgHist.capacity) msgHist.dequeue()
175-
176-
// successful query, save it in context history
177-
msgHist.enqueue({
178-
role: 'assistant',
179-
content: response,
180-
images: messageAttachment || []
181-
})
152+
const model: string = userConfig.options['switch-model']
182153

183-
// only update the json on success
184-
openChannelInfo(message.channelId,
185-
message.channel as TextChannel,
186-
message.author.tag,
187-
msgHist.getItems()
188-
)
189-
} catch (error: any) {
190-
msgHist.pop() // remove message because of failure
191-
message.reply(`**Error Occurred:**\n\n**Reason:** *${error.message}*`)
192-
}
154+
// set up new queue
155+
msgHist.setQueue(chatMessages)
156+
157+
// check if we can push, if not, remove oldest
158+
while (msgHist.size() >= msgHist.capacity) msgHist.dequeue()
159+
160+
// push user response before ollama query
161+
msgHist.enqueue({
162+
role: 'user',
163+
content: cleanedMessage,
164+
images: messageAttachment || []
165+
})
166+
167+
// response string for ollama to put its response
168+
const response: string = await normalMessage(message, ollama, model, msgHist, shouldStream)
169+
170+
// If something bad happened, remove user query and stop
171+
if (response == undefined) { msgHist.pop(); return }
172+
173+
// if queue is full, remove the oldest message
174+
while (msgHist.size() >= msgHist.capacity) msgHist.dequeue()
175+
176+
// successful query, save it in context history
177+
msgHist.enqueue({
178+
role: 'assistant',
179+
content: response,
180+
images: messageAttachment || []
181+
})
182+
183+
// only update the json on success
184+
openChannelInfo(message.channelId,
185+
message.channel as TextChannel,
186+
message.author.tag,
187+
msgHist.getItems()
188+
)
189+
} catch (error: any) {
190+
msgHist.pop() // remove message because of failure
191+
message.reply(`**Error Occurred:**\n\n**Reason:** *${error.message}*`)
193192
}
194-
)
193+
})

0 commit comments

Comments
 (0)