|
| 1 | +require("dotenv").config(); |
| 2 | + |
| 3 | +exports.run = async (client, msg, args) => { |
| 4 | + let trusted = client.trusted.find(usr => (usr == msg.author.id)); |
| 5 | + if (!trusted) return msg.channel.send("Apologies, you are not allowed to talk to my friend Kyle3.5.") |
| 6 | + |
| 7 | + const username = String(msg.author.username); |
| 8 | + const embedQuestion = new client.discord.MessageEmbed(); |
| 9 | + const embedResult = new client.discord.MessageEmbed(); |
| 10 | + embedResult.setColor("RANDOM"); |
| 11 | + |
| 12 | + embedQuestion.setColor("RANDOM"); |
| 13 | + embedQuestion.addField("You asked ChatGPT...", args.join(" ")); |
| 14 | + embedQuestion.setFooter({ |
| 15 | + text: "Please wait for a response...Might take a few seconds...", |
| 16 | + iconURL: msg.author.avatarURL() |
| 17 | + }); |
| 18 | + |
| 19 | + msg.channel.send({ |
| 20 | + embeds: [embedQuestion] |
| 21 | + }).then(async (msgEdit) => { |
| 22 | + |
| 23 | + const completion = await client.openai.createChatCompletion({ |
| 24 | + model: "gpt-3.5-turbo", |
| 25 | + messages: [{ |
| 26 | + role: "user", |
| 27 | + content: args.join() |
| 28 | + }], |
| 29 | + max_tokens: 500, |
| 30 | + }).catch(err => { |
| 31 | + console.error("Err GPT:", err); |
| 32 | + embedResult.setDescription(`Uh oh, ${err.type}: ${err.message} ${err?.code}`); |
| 33 | + }); |
| 34 | + |
| 35 | + if (completion && completion?.data) { |
| 36 | + const choices = completion.data; |
| 37 | + const response = choices.choices[0].message; |
| 38 | + console.log(response); |
| 39 | + const gptResponse = segmentation(response.content) |
| 40 | + |
| 41 | + // embedResult.addField("You asked DaVinci...", args.join(" ")) |
| 42 | + for (let index = 0; index < gptResponse.length; index++) { |
| 43 | + if (index >= 16) embedResult.addField(`Too long`, `Apologies, have to refine code to indiacte more messages...`) |
| 44 | + else embedResult.addField(index === 0 ? `ChatGPT ${response.role}` : "\u200b", gptResponse[index]) |
| 45 | + } |
| 46 | + } else { |
| 47 | + embedResult.addField(`Oopsie`, "No Result") |
| 48 | + } |
| 49 | + |
| 50 | + embedResult.setFooter({ |
| 51 | + text: username + ", this is an unfinished bot- it doesn't use previous answers yet.", |
| 52 | + iconURL: msg.author.avatarURL() |
| 53 | + }) |
| 54 | + embedResult.setColor("RANDOM") |
| 55 | + |
| 56 | + msgEdit.edit({ |
| 57 | + embeds: [embedResult] |
| 58 | + }) |
| 59 | + }) |
| 60 | +} |
| 61 | + |
| 62 | +const segmentation = (phrase) => { |
| 63 | + // return phrase.match(/[\s\S]{1,1020}/gm) || []; |
| 64 | + return phrase.trim().match(/.{1,1024}(\\s|$)/gm) || []; |
| 65 | +} |
| 66 | + |
| 67 | +exports.help = { |
| 68 | + name: 'chat', |
| 69 | + desc: "chat using ChatGPT algorithm: gpt3.5-turbo", |
| 70 | + type: "Fun", |
| 71 | + usage: "chat Please tell me a short bedtime story", |
| 72 | + owner: false, |
| 73 | + locked: false, |
| 74 | + guild: false |
| 75 | +} |
0 commit comments