forked from onury5506/Discord-ChatGPT-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
84 lines (73 loc) · 2.79 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import 'dotenv/config'
import { Client, GatewayIntentBits, Partials, ChannelType } from 'discord.js'
import { initChatGPT, askQuestion } from './chatgpt/chatgpt.js'
import { initDiscordCommands, handle_interaction_ask, handle_interaction_image, handle_interaction_remix } from './discord/discord_commands.js'
import { splitAndSendResponse, MAX_RESPONSE_CHUNK_LENGTH } from './discord/discord_helpers.js'
import Conversations from './chatgpt/conversations.js'
async function main() {
await initChatGPT().catch(e => {
console.error(e)
process.exit()
})
await initDiscordCommands()
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildIntegrations,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.DirectMessageTyping,
GatewayIntentBits.MessageContent
],
partials: [Partials.Channel]
});
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
console.log(new Date())
});
client.on("messageCreate", async message => {
if (process.env.ENABLE_DIRECT_MESSAGES !== "true" || message.channel.type != ChannelType.DM || message.author.bot) {
return;
}
const user = message.author
console.log("----Direct Message---")
console.log("Date : " + new Date())
console.log("UserId : " + user.id)
console.log("User : " + user.username)
console.log("Message : " + message.content)
console.log("--------------")
if (message.content.toLowerCase() == "reset") {
Conversations.resetConversation(user.id)
user.send("Wer bist du?")
return;
}
let conversationInfo = Conversations.getConversation(user.id)
try {
let sentMessage = await user.send("Hmm, lass mich nachdenken..")
askQuestion(message.content, async (response) => {
if (response.length >= MAX_RESPONSE_CHUNK_LENGTH) {
splitAndSendResponse(response, user)
} else {
await sentMessage.edit(response)
}
}, { conversationInfo })
} catch (e) {
console.error(e)
}
})
client.on("interactionCreate", async interaction => {
switch (interaction.commandName) {
case "ask":
handle_interaction_ask(interaction)
break;
case "image":
handle_interaction_image(interaction)
break
case "remix":
handle_interaction_remix(interaction,client)
break
}
});
client.login(process.env.DISCORD_BOT_TOKEN);
}
main()