Skip to content

Commit ea309c4

Browse files
committed
removes chatgpt entirely, and fixes startup not working
1 parent eed701d commit ea309c4

File tree

7 files changed

+3
-190
lines changed

7 files changed

+3
-190
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ Clone the repository, `cd` into it and run `npm install`.
1414
## ➤ Features
1515

1616
- [x] Calendar
17-
- [x] ChatGPT
1817
- [x] Exams
1918
- [x] Mensa
2019
- [x] Multi Language Support
@@ -58,6 +57,5 @@ The only required value is `botToken`, which you can get from the [Discord Devel
5857
"calendar_2": "<your calendar url>",
5958
},
6059
"botToken": "<your bot token>",
61-
"openai_token": "<your openai token>",
6260
}
6361
```

events/messageCreate.ts

Lines changed: 1 addition & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -2,94 +2,12 @@ import { EmbedBuilder, MessageCreateOptions, MessagePayload, TextChannel } from
22
import tx2 from 'tx2'
33
import { DiscordClient, DiscordMessage } from '../types/customTypes'
44

5-
/**
6-
* Custom PM2 metric.
7-
*/
8-
const openai_api_requests = tx2.counter('OpenAI-API Requests')
9-
105
exports.run = (client: DiscordClient, message: DiscordMessage) => {
116
/**
127
* Only respond to messages sent by users.
138
*/
149
if (message.author.bot) return
1510

16-
/**
17-
* OpenAI's Chat-GPT
18-
*/
19-
if (message.channel.isThread() && message.channel.name === 'chat-gpt') {
20-
try {
21-
let requestDone = false
22-
let openai_api_request_sent = false
23-
24-
console.log(`Chat-GPT request, by: ${message.author.username}`)
25-
/**
26-
* Function to wait for the API response and send typing.
27-
*/
28-
const awaiting_api_reposonse = () => {
29-
setTimeout(() => {
30-
/**
31-
* Send typing indicator.
32-
*/
33-
message.channel.sendTyping()
34-
35-
if (openai_api_request_sent === false) {
36-
openai_api_request_sent = true
37-
client.openai.chat.completions
38-
.create({
39-
model: 'gpt-3.5-turbo',
40-
messages: [{ role: 'user', content: message.content.toString() }],
41-
})
42-
.then(request => {
43-
/**
44-
* Set requestDone to true, so the request doesn't get sent again.
45-
*/
46-
requestDone = true
47-
48-
/**
49-
* Split the message into multiple messages if it's too long.
50-
*/
51-
splitMessageRegex(request.choices[0].message.content.toString(), {
52-
maxLength: 2000,
53-
prepend: '',
54-
append: '',
55-
}).forEach((element: string | MessagePayload | MessageCreateOptions) => {
56-
message.channel.send(element)
57-
})
58-
59-
openai_api_requests.inc(1)
60-
console.log(`Chat-GPT request done. Request by: ${message.author.username}`)
61-
})
62-
}
63-
if (requestDone === false) {
64-
/**
65-
* If the request is not done, wait 1 second and send typing again.
66-
*/
67-
awaiting_api_reposonse()
68-
}
69-
}, 1000)
70-
}
71-
72-
awaiting_api_reposonse()
73-
} catch (error) {
74-
if (error.response) {
75-
console.log(`Error status: ${error.response.status}`)
76-
console.log(`Error data: ${error.response.data}`)
77-
message.channel.send(
78-
client.translate({
79-
key: 'interactions.chatgpt.error',
80-
options: {
81-
error_status: error.response.status,
82-
error_data: error.response.data,
83-
lng: message.author.language,
84-
},
85-
}),
86-
)
87-
} else {
88-
console.log(`Error message: ${error.message}`)
89-
}
90-
}
91-
}
92-
9311
/**
9412
* DM handling and forwarding.
9513
*/
@@ -132,71 +50,4 @@ exports.run = (client: DiscordClient, message: DiscordMessage) => {
13250
throw new Error(error)
13351
}
13452
}
135-
}
136-
137-
138-
/**
139-
* A function for splitting a string into fixed-length parts. Designed as a
140-
* workaround to an issue in the discord.js Util.splitMessage function
141-
* https://github.com/discordjs/discord.js/issues/7674
142-
* @param {string} text The string to split into multiple messages, each of
143-
* which will be no longer than maxLength
144-
* @param {number} [options.maxLength] The maximum number of characters in each
145-
* string in the returned array
146-
* @param {RegExp} [options.regex] A global regex which matches the delimeters on
147-
* which to split text when needed to keep each part within maxLength
148-
* @param {string} [options.prepend] A string to add before each part iff
149-
* text is split into multiple parts
150-
* @param {string} [options.append] A string to add after each part iff text
151-
* is split into multiple parts
152-
* @returns {string[]} An array of strings which are substrings of text, split
153-
* using options.regex, combined such that each part is as long as possible
154-
* while not exceeding options.maxLength.
155-
*/
156-
function splitMessageRegex(
157-
text: string,
158-
{
159-
maxLength = 2_000,
160-
regex = /\n/g,
161-
prepend = '',
162-
append = '',
163-
}: { maxLength?: number; regex?: RegExp; prepend?: string; append?: string } = {},
164-
): any[] {
165-
if (text.length <= maxLength) return [text]
166-
const parts = []
167-
let curPart = prepend
168-
let chunkStartIndex = 0
169-
170-
let prevDelim = ''
171-
172-
function addChunk(chunkEndIndex: number, nextDelim: string) {
173-
const nextChunk = text.substring(chunkStartIndex, chunkEndIndex)
174-
const nextChunkLen = nextChunk.length
175-
176-
// If a single part would exceed the length limit by itself, throw an error:
177-
if (prepend.length + nextChunkLen + append.length > maxLength) {
178-
throw new RangeError('SPLIT_MAX_LEN')
179-
}
180-
181-
// The length of the current part if the next chunk were added to it:
182-
const lengthWithChunk = curPart.length + prevDelim.length + nextChunkLen + append.length
183-
184-
// If adding the next chunk to the current part would cause it to exceed
185-
// the maximum length, push the current part and reset it for next time:
186-
if (lengthWithChunk > maxLength) {
187-
parts.push(curPart + append)
188-
curPart = prepend + nextChunk
189-
} else {
190-
curPart += prevDelim + nextChunk
191-
}
192-
prevDelim = nextDelim
193-
chunkStartIndex = chunkEndIndex + prevDelim.length
194-
}
195-
196-
for (const match of text.matchAll(regex)) {
197-
addChunk(match.index, match[0])
198-
}
199-
addChunk(text.length - 1, '')
200-
parts.push(curPart + append)
201-
return parts
202-
}
53+
}

index.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as fs from 'fs'
22
import { Collection, GatewayIntentBits, Partials } from 'discord.js'
33
import i18next from 'i18next'
44
import Backend from 'i18next-fs-backend'
5-
import OpenAI from 'openai'
65
import ids from './private/ids.json'
76
import sensitive from './private/sensitive.json'
87
import settings from './private/settings.json'
@@ -65,19 +64,6 @@ client.config = {
6564
*/
6665
client.login(client.config.sensitive.botToken)
6766

68-
if (client.config.sensitive.openai_token) {
69-
/*
70-
* Create OpenAIApi instance
71-
*/
72-
73-
74-
client.openai = new OpenAI({
75-
apiKey: client.config.sensitive.openai_token
76-
})
77-
} else {
78-
console.error('client.config.sensitive.openai_token is undefined')
79-
}
80-
8167
/**
8268
* Load and run events.
8369
*/

locales/de.json

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,6 @@
1212
"parent": "Oberkategorie",
1313
"type": "Typ"
1414
},
15-
"chatgpt": {
16-
"already_in_thread": "Ich kann keinen Thread erstellen, da du dich bereits in einem befindest.",
17-
"cannot_create_thread": "*Chat-GPT* Thread konnte nicht erstellt werden.",
18-
"error": "Ein Fehler ist aufgetreten. Bitte versuche es später erneut.\nWenn dieser Fehler weiterhin besteht, kontaktiere bitte den Bot-Entwickler.\n\nFehler Status: {{error}}\nFehler Daten: {{error_data}}",
19-
"localized_description": "chats in gpt",
20-
"localized_name": "chatgpt",
21-
"thread_created": "*Chat-GPT* Thread erstellt!",
22-
"thread_message": "Chat-GPT liegt zu deinen Füßen, du kannst mich alles fragen.\nNach 60min wird der Kanal archiviert"
23-
},
2415
"fact": {
2516
"fact": "🧠Fakt",
2617
"localized_description": "Spuckt einen zufälligen Fakt aus.",
@@ -142,4 +133,4 @@
142133
},
143134
"language": "Deutsch",
144135
"missingPermission": "Du bist nicht berechtigt, diesen Befehl auszuführen."
145-
}
136+
}

locales/en-US.json

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,6 @@
1212
"parent": "Parent",
1313
"type": "Type"
1414
},
15-
"chatgpt": {
16-
"already_in_thread": "Cannot create thread since you already are in a thread channel.",
17-
"cannot_create_thread": "Cannot create *Chat-GPT* thread.",
18-
"error": "An error occured. Please try again later.\nIf this error persists, please contact the bot owner.\n\nError Status: {{error}}\nError data: {{error_data}}",
19-
"localized_description": "chats in gpt",
20-
"localized_name": "chatgpt",
21-
"thread_created": "*Chat-GPT* thread successfully created!",
22-
"thread_message": "Chat-GPT at your service, ask me anything.\nAfter 60mins of inactivity this channel will be archived."
23-
},
2415
"fact": {
2516
"fact": "🧠Fact",
2617
"localized_description": "Spits out a random fact.",
@@ -142,4 +133,4 @@
142133
},
143134
"language": "English",
144135
"missingPermission": "You do not have permission to perform that command."
145-
}
136+
}

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
"moment-timezone": "^0.5.46",
2727
"node-ical": "^0.19.0",
2828
"node-schedule": "^2.1.1",
29-
"openai": "^4.68.1",
3029
"os": "^0.1.2",
3130
"pm2": "^5.4.2",
3231
"tenorjs": "^1.0.10",

types/customTypes.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import {
2525
} from 'discord.js'
2626
import i18next, { TOptions } from 'i18next'
2727
import { CalendarResponse } from 'node-ical'
28-
import OpenAI from 'openai'
2928

3029
/**
3130
* Extended version of the default {@link Client} with addidtional functions and properties.
@@ -126,7 +125,6 @@ export class DiscordClient extends Client {
126125
*/
127126
public interactions: Collection<string, InteractionCommands>
128127

129-
public openai: OpenAI
130128
/**
131129
* Uses {@link Message.reply()} to reply to the issued command.
132130
* @param {Message} message message to ryply to
@@ -393,7 +391,6 @@ interface config_sensitive_typing {
393391
}
394392
calendars: { [key: string]: string }
395393
botToken: string
396-
openai_token?: string
397394
}
398395

399396
interface config_ids_typing {

0 commit comments

Comments
 (0)