diff --git a/commands/API/chat.js b/commands/API/chat.js new file mode 100644 index 0000000..6440bb6 --- /dev/null +++ b/commands/API/chat.js @@ -0,0 +1,70 @@ +const { MongoClient, ServerApiVersion, ConnectionClosedEvent } = require('mongodb'); +const { exit } = require('process'); + + +async function getResponse(convo, bot) { + const response = await bot.openai.createCompletion({ + model: "text-davinci-002", + prompt: convo, + temperature: 0.9, + max_tokens: 150, + top_p: 1, + frequency_penalty: 0, + presence_penalty: 0.6, + stop: [" Human:", " AI:"], + }); + + return response; +} + +async function convoManager(clientinp, bot, message) { + //Just in case, make sure it can't be changed + const client = clientinp; + const dbo = client.db("DM").collection(message.author.id); + + if (message.content.startsWith('!')) { + if (message.content.split(' ')[0] == '!startconvo') { + //Check if a conversation already exists + dbo.find({'_id': {$exists: true}}).toArray((err, docs) => { + if (docs[0] != undefined) { + return message.reply("You're already in a conversation"); + } else { + dbo.insertOne({convo: 'Human: Hello\nAI: Hello'}); + return message.channel.send('-----Started Conversation-----\nuse _!endconvo_ to end the conversation!\n\n_Disclaimer: Your conversation data is stored for the duration of the conversation to help Selmer Bot better understand what you are saying *then deleted*_\n\n'); + } + }); + + } else if (message.content.split(' ')[0] == '!endconvo') { + dbo.drop(); + return message.channel.send('-----Ended Conversation-----\nSee you next time!'); + } else { + return message.reply('UNUSABLE DM COMMAND DETECTED'); + } + } else { + dbo.find({convo: {$exists: true}}).toArray(async function (err, docs) { + const doc = docs[0]; + if (!doc) { return message.reply('You aren\'t currently in a conversation\nUse _!startconvo_ to start one!'); } + + let convo = doc.convo; + convo += `\nHuman: ${message.content}\n`;; + + //Get the response + const r = await getResponse(convo, bot); + + let response = r.data.choices[0].text; + + convo += (response + '\n'); + + dbo.updateOne(doc, {$set: {convo: convo}}); + response = response.replaceAll('AI: ', '').replaceAll('AI:\n', ''); + console.log(response); + message.reply(response); + }); + } +} +//"Hello! discord_user:" +module.exports = { + name: 'chat', + description: 'chat', + convoManager +} \ No newline at end of file diff --git a/commands/dm_handler.js b/commands/dm_handler.js new file mode 100644 index 0000000..96f3de6 --- /dev/null +++ b/commands/dm_handler.js @@ -0,0 +1,34 @@ +const { convoManager } = require('./API/chat.js'); +const { MongoClient, ServerApiVersion, ConnectionClosedEvent } = require('mongodb'); + +function handle_dm(message, bot) { + if (message.author.bot) { return; } + + if (!message.content.startsWith('!') || message.content.split(' ')[0] == '!startconvo' || message.content.split(' ')[0] == '!endconvo') { + const member = bot.guilds.cache.get(bot.home_server).members.cache.get(message.author.id); + + const client = new MongoClient(bot.mongouri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 }); + client.connect(async (err) => { + if (err) { return console.log(err); } + + const dbo = client.db('main').collection('authorized'); + dbo.find({id: message.author}).toArray((err, docs) => { + + //Only available to Selmer Bot devs, testers and "authorized" users + if (docs[0] != undefined || member.roles.cache.has('944048889038774302') || member.roles.cache.has('946610800418762792')) { + convoManager(client, bot, message); + } + }); + }); + + client.close(); + } else { + return message.reply('UNUSABLE DM COMMAND DETECTED'); + } + + //Selmer Bot is conversing with them + +} + + +module.exports = { handle_dm } \ No newline at end of file diff --git a/main.js b/main.js index 08441d2..4281950 100644 --- a/main.js +++ b/main.js @@ -2,9 +2,13 @@ const { Client, Intents, MessageActionRow, MessageButton, MessageSelectMenu } = const Discord = require('discord.js'); const { MongoClient, ServerApiVersion } = require('mongodb'); const fs = require('fs'); +// const OpenAI = require('openai-api') +const { Configuration, OpenAIApi } = require("openai"); + const turnManager = require('./commands/turnManager.js'); const { welcome } = require('./commands/admin/welcome.js'); const { handle_interaction } = require('./commands/interactionhandler.js'); +const { handle_dm } = require('./commands/dm_handler'); const { exit } = require('process'); const BASE_LVL_XP = 20; @@ -14,14 +18,20 @@ const BASE_LVL_XP = 20; let token; let IDM = false; let home_server; + +let MLAIKEY; + + if (process.env.token != undefined) { //Use "setx NAME VALUE" in the local powershell terminal to set token = process.env.token; home_server = process.env.home_server; + MLAIKEY = process.env.MLAIKEY; } else { token = require('./config.json').token; home_server = require('./config.json').home_server; IDM = true; + MLAIKEY = new require('./config.json').MLAIKEY; } // const { token } = require('./config.json'); @@ -36,8 +46,12 @@ const bot = new Client({ Intents.FLAGS.GUILD_VOICE_STATES, Intents.FLAGS.GUILD_EMOJIS_AND_STICKERS, Intents.FLAGS.GUILD_PRESENCES, - Intents.FLAGS.GUILD_MEMBERS + Intents.FLAGS.GUILD_MEMBERS, + Intents.FLAGS.DIRECT_MESSAGES, + Intents.FLAGS.DIRECT_MESSAGE_REACTIONS, + Intents.FLAGS.DIRECT_MESSAGE_TYPING, ], + partials: [ 'CHANNEL' ] }); const prefix = '!'; @@ -46,6 +60,12 @@ bot.prefix = prefix; bot.inDebugMode = IDM; bot.home_server = home_server; +const configuration = new Configuration({ + apiKey: MLAIKEY, +}); +bot.openai = new OpenAIApi(configuration); +bot.temptext = ''; + //MongoDB integration //Development support @@ -56,6 +76,7 @@ if (process.env.MONGODB_URI) { mongouritemp = require('./config.json'); } const mongouri = mongouritemp; +bot.mongouri = mongouri; const { connect } = require('mongoose'); bot.on("guildCreate", guild => { @@ -199,6 +220,11 @@ bot.on('guildMemberAdd', async (member) => { bot.on('messageCreate', (message) => { + //DM SECTION + if (message.channel.type === "DM") { + return handle_dm(message, bot); + } + //Special case, testing server (still need the emojis) if (!bot.inDebugMode && message.guild.id == bot.home_server) { return; }