-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
174 lines (149 loc) · 4.01 KB
/
main.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
const {
app,
clipboard,
globalShortcut,
BrowserWindow,
ipcMain,
} = require("electron");
const prompt = require("electron-prompt");
const discord = require("discord.js");
require("dotenv").config({ path: "./config/.env" });
let parentMessageId = "";
let conversationId = "";
let registered = false;
let pendingRequest = false;
let mainWindow;
let api;
let webhookClient;
let promptTitles = false;
function createWindow() {
mainWindow = new BrowserWindow({
width: 1100,
height: 1000,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
},
});
mainWindow.setMenuBarVisibility(false);
mainWindow.loadFile("./html/config.html");
mainWindow.on("closed", () => {
mainWindow = null;
});
}
app.whenReady().then(async () => {
const { ChatGPTUnofficialProxyAPI } = await import("chatgpt");
api = new ChatGPTUnofficialProxyAPI({
accessToken: process.env.ACCESS_TOKEN,
apiReverseProxyUrl: process.env.API_REVERSE_PROXY_URL,
});
createWindow();
app.on("activate", async () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});
ipcMain.on(
"register",
async (event, message, discordWebhookURL, promptTitlesInput) => {
if (pendingRequest) {
mainWindow.webContents.send(
"error",
"Please wait for the conversation to be ready..."
);
} else {
pendingRequest = true;
try {
const res = await api.sendMessage(message);
conversationId = res.conversationId;
parentMessageId = res.id;
registered = true;
if (discordWebhookURL) {
webhookClient = new discord.WebhookClient({ url: discordWebhookURL });
}
promptTitles = promptTitlesInput;
mainWindow.loadFile("./html/translate.html");
setTimeout(() => {
mainWindow.webContents.send(
"console",
`Please use "Command+Shift+}" to translate the current clipboard.\nIt will be outputted to ${
discordWebhookURL
? "Discord"
: "the console when using this keybind"
}`
);
globalShortcut.register("CommandOrControl+Shift+}", () => {
translate();
});
}, 300);
} catch (err) {
mainWindow.webContents.send(
"error",
"There was an error registering the chat."
);
}
pendingRequest = false;
}
}
);
app.on("will-quit", () => {
globalShortcut.unregisterAll();
});
async function translate() {
if (!registered) return;
if (pendingRequest) {
mainWindow.webContents.send(
"console",
"Please wait for the current request to finish before making another request!"
);
return;
}
const clipboardText = clipboard.readText();
if (clipboardText.length === 0) return;
let title;
if (promptTitles) {
try {
const response = await prompt({
title: "Translation Title",
label: "Title:",
inputAttrs: {
type: "text",
},
type: "input",
});
if (response != null) title = response;
} catch (error) {
console.error(error);
return;
}
}
pendingRequest = true;
try {
mainWindow.webContents.send("console", "\n\nTranslating...");
const lowercase = clipboardText.toLowerCase();
const cleaned = lowercase.replace(/- /g, "");
const res = await api.sendMessage(cleaned, {
conversationId,
parentMessageId,
});
if (webhookClient) {
mainWindow.webContents.send("console", "\n\nSending to Discord...");
webhookClient.send(`${title ? title : ""}\`\`\`${res.text}\`\`\``);
} else {
mainWindow.webContents.send(
"console",
"\n\nReturned Translation:\n" + (title ? title : "") + "\n" + res.text
);
}
pendingRequest = false;
parentMessageId = res.id;
} catch (err) {
mainWindow.webContents.send("console", err);
}
}