-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTeX2image.js
121 lines (108 loc) · 3.9 KB
/
TeX2image.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
const channel_name = "β-botテスト"; //channel_nameのチャンネルだけにbotは反応する
const { Client, GatewayIntentBits } = require("discord.js");
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMessages,
],
});
require("dotenv").config();
const token = process.env.token;
const { Worker } = require("worker_threads");
let worker = new Worker("./TeX2image_worker.js");
async function worker_on_msg(obj) {
if (obj.msg === "") {
client.channels.cache
.find((ch) => ch.name === channel_name)
?.send({ files: ["./" + obj.name] });
} else {
client.channels.cache
.find((ch) => ch.name === channel_name)
?.send(obj.msg.toString());
}
}
worker.on("message", worker_on_msg);
client.on("ready", () => {
console.log(`${client.user.tag} でログインしています。`);
});
client.on("messageCreate", async (msg) => {
if (msg.channel.name !== channel_name) return;
if (msg.content === "!!ping") {
msg.reply("Pong!");
} else if (msg.content === "!!destroy") {
worker.terminate();
client.destroy();
} else if (msg.content === "!!help") {
client.channels.cache
.find((ch) => ch.name === channel_name)
?.send(
"> 〇このbot is 何?\n" +
"> TeX記法で書かれた文字列から画像を生成するbotです。\n\n" +
"> 〇使用方法\n" +
"> botがオンライン時にコマンドを打ち込むと、コマンドに対応した動作をします。\n\n" +
"> 〇コマンド一覧\n> \n" +
"> ・!!ping\n> Pong!と返します。\n> \n" +
"> ・!!destroy\n> botがログオフします。\n> \n" +
"> ・!!help\n> コマンド一覧を表示します。\n> \n" +
"> ・!!TeX filename.type option \\`\\`\\`str\\`\\`\\`\n> TeXという記法で書かれた文字列strからflilename.typeの画像を生成します。typeはsvg,png,jpeg,webpのいずれかです。optionでは背景透過の有無を決めます。flilename.typeとoptionは省略できます。"
);
} else {
//!!TeX filetype option str
if (msg.content.length >= 5) {
let posObj = { name: "file.png", type: "png", option: false, str: "" };
let commands_list = [];
let ch = msg.content.indexOf("```");
if (ch === -1) return;
let i = 0;
while (ch > i) {
let j = msg.content.indexOf(" ", i);
if (j === -1) break;
commands_list.push(msg.content.substring(i, j));
i = j + 1;
}
commands_list.push("");
i = 0;
let len = commands_list.length - 1;
if (i > len) return;
if (commands_list[i] !== "!!TeX") return;
i++;
if (i > len) msg.reply("command error");
if (commands_list[i].indexOf(".svg") !== -1) {
posObj.name = commands_list[i];
posObj.type = "svg";
i++;
} else if (commands_list[i].indexOf(".png") !== -1) {
posObj.name = commands_list[i];
posObj.type = "png";
i++;
} else if (commands_list[i].indexOf(".jpeg") !== -1) {
posObj.name = commands_list[i];
posObj.type = "jpeg";
i++;
} else if (commands_list[i].indexOf(".webp") !== -1) {
posObj.name = commands_list[i];
posObj.type = "webp";
i++;
}
if (i > len) msg.reply("command error");
if (commands_list[i] === "true") {
posObj.option = true;
i++;
} else if (commands_list[i] === "false") {
posObj.option = false;
i++;
}
if (posObj.type === "jpeg" && posObj.option === true)
posObj.option = false;
len = msg.content.length;
ch += 3;
i = msg.content.indexOf("```", ch);
if (i === -1) msg.reply("command error");
posObj.str = msg.content.substring(ch, i);
worker.postMessage(posObj);
}
}
});
//ログイン
client.login(token);