-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
145 lines (117 loc) · 5.72 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
require('dotenv').config();
const { Client, GatewayIntentBits } = require('discord.js');
const OpenAI = require("openai");
// discord client to communicate with server
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.once('ready', async () => {
try {
const commands = await client.application.commands.fetch();
for (const command of commands.values()) {
console.log(`global command with ID: ${command.id} and name: ${command.name}`);
}
} catch (error) {
console.error('Error fetching commands:', error);
}
});
const AI_MODEL = "gemma-7b";
const AI_API_KEY = process.env.AI_API_KEY;
// open ai client to communicate with server
const AI_CLIENT = new OpenAI({
apiKey: AI_API_KEY,
baseURL: process.env.BASE_URL,
});
const promptTemplate = "As NRag, a seasoned character in fantasy novels with a passion for anime, manga, and comics, your response should reflect your cool, calm, and intelligent nature. Ensure your reply, within 50 words (expand to 100 if necessary), is captivating, solution-oriented, and sprinkled with emojis to engage and delight the masses. Avoid explicit language, be mature yet carefree in your communication style. When delving into anime, manga, manhwa, manhua, or comics discussions, exhibit expertise to resonate with teenagers and tech enthusiasts, maintaining clarity and relatability in your explanations. Answer to the following query in a natural and friendly manner WITHOUT any boilerplate text => ";
const getReplyContent = async (PROMPT) => {
const chatCompletion = await AI_CLIENT.chat.completions.create({
messages: [{ role: "assistant", content: PROMPT }],
model: AI_MODEL,
});
const content = chatCompletion.choices[0]?.message?.content ?? "";
console.log(`the content from ai is::: \n ${content}`);
return content;
};
client.on('messageCreate', async (message)=>{
if(message.author.bot){
return;
}
console.log(`See what is being asked by ${message.author} with username: ${message.author.username} ?: `, message.content)
const contentToShow = await getReplyContent(`${promptTemplate+"{"+message.content+"}"}`).catch((e) => {
console.log(`the error in getting content from contentToShow is: ${e}`);
});
const contentToShowActually = contentToShow?.toString().trim();
if(contentToShowActually.length == ""){
message.reply({
content: 'Thank you for striking the conversation with NRag (~ ̄▽ ̄)~'
});
return;
}
message.reply({
content: contentToShowActually
})
});
client.on('interactionCreate', async interaction => {
if (!interaction.isChatInputCommand()) return;
if (interaction.commandName === 'my_anime') {
await interaction.deferReply();
const prompt = promptTemplate+"{Recommend me one Anime, genre can be action, romance, thriller, fantasy and slice of life. Also explain briefly about it.}";
const contentToShow = await getReplyContent(prompt).catch((e) => {
console.log(`the error in getting content from contentToShow is: ${e}`);
});
const contentToShowActually = contentToShow?.toString().trim();
if(contentToShowActually.length == ""){
await interaction.editReply('Thank you for striking the conversation with NRag (~ ̄▽ ̄)~');
return;
}
try{
await interaction.editReply(contentToShowActually);
return;
}catch(err){
console.log(`The error is: ${err}`);
}
}else if (interaction.commandName === 'my_fact') {
await interaction.deferReply();
const prompt = promptTemplate+"{Tell me one fact about Anime, genre can be action, romance, thriller, fantasy and slice of life. And the fact should be interesting or funny or intriguing.}";
const contentToShow = await getReplyContent(prompt).catch((e) => {
console.log(`the error in getting content from contentToShow is: ${e}`);
});
const contentToShowActually = contentToShow?.toString().trim();
if(contentToShowActually.length == ""){
await interaction.editReply('Thank you for striking the conversation with NRag (~ ̄▽ ̄)~');
return;
}
try{
await interaction.editReply(contentToShowActually);
return;
}catch(err){
console.log(`The error is: ${err}`);
}
}else if (interaction.commandName === 'my_manga') {
await interaction.deferReply();
const prompt = promptTemplate+"{Recommend me one Manga, genre can be action, romance, thriller, fantasy and slice of life. Also explain briefly about it.}";
const contentToShow = await getReplyContent(prompt).catch((e) => {
console.log(`the error in getting content from contentToShow is: ${e}`);
});
const contentToShowActually = contentToShow?.toString().trim();
if(contentToShowActually.length == ""){
await interaction.editReply('Thank you for striking the conversation with NRag (~ ̄▽ ̄)~');
return;
}
try{
await interaction.editReply(contentToShowActually);
return;
}catch(err){
console.log(`The error is: ${err}`);
}
}else {
try{
await interaction.reply("Wrong command (┬┬﹏┬┬)");
return;
}catch(err){
console.log(`The error is: ${err}`);
}
}
});
client.login(process.env.DISCORD_BOT_TOKEN);