|
| 1 | +import { ChatGPTAPI, getOpenAIAuth } from "chatgpt"; |
| 2 | +import tmi from "tmi.js"; |
| 3 | +import dotenv from "dotenv"; |
| 4 | +dotenv.config(); |
| 5 | + |
| 6 | +const client = new tmi.Client({ |
| 7 | + options: { debug: true }, |
| 8 | + connection: { |
| 9 | + secure: true, |
| 10 | + reconnect: true, |
| 11 | + }, |
| 12 | + identity: { |
| 13 | + username: process.env.TWITCH_NAME, |
| 14 | + password: process.env.TWITCH_TOKEN, |
| 15 | + }, |
| 16 | + channels: ["chatgpt_bot_0001"], |
| 17 | +}); |
| 18 | + |
| 19 | +client.connect(); |
| 20 | + |
| 21 | +client.on("message", (channel, tags, message, self) => { |
| 22 | + if (self || !message.startsWith("!")) { |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + const args = message.slice(1).split(" "); |
| 27 | + const command = args.shift()?.toLowerCase(); |
| 28 | + |
| 29 | + if (command === "!chatgpt test") { |
| 30 | + client.say(channel, `@${tags.username}, test`); |
| 31 | + } else if (command === "chatgpt") { |
| 32 | + (async () => { |
| 33 | + const prompt = args.join(" "); |
| 34 | + |
| 35 | + const openAIAuth = await getOpenAIAuth({ |
| 36 | + email: process.env.OPENAI_EMAIL, |
| 37 | + password: process.env.OPENAI_PASSWORD, |
| 38 | + }); |
| 39 | + |
| 40 | + const api = new ChatGPTAPI({ ...openAIAuth }); |
| 41 | + await api.ensureAuth(); |
| 42 | + |
| 43 | + const response = await api.sendMessage(prompt); |
| 44 | + |
| 45 | + client.say(channel, `@${tags.username}, ${response}}`); |
| 46 | + })(); |
| 47 | + } |
| 48 | +}); |
0 commit comments