148 lines
3.4 KiB
TypeScript
148 lines
3.4 KiB
TypeScript
import * as fs from "fs";
|
||
import config from "../../config.js";
|
||
import type { ueuse } from "types/types";
|
||
|
||
const initialFile: Array<string> = [];
|
||
|
||
// コマンド読み込み
|
||
import Info from "./info.js";
|
||
import Follow from "./follow.js";
|
||
import UnFollow from "./unfollow.js";
|
||
import Weather from "./weather.js";
|
||
import Help from "./help.js";
|
||
import Report from "./report.js";
|
||
import Terms from "./legal/terms.js"
|
||
import PrivacyPolicy from "./legal/privacy.js";
|
||
|
||
// 初期化
|
||
if (!fs.existsSync("data/alreadyCommands.json")) {
|
||
fs.writeFileSync(
|
||
"data/alreadyCommands.json",
|
||
JSON.stringify(initialFile),
|
||
"utf-8",
|
||
);
|
||
}
|
||
|
||
// 対応済みユーズ一覧
|
||
const alreadyCommands: Array<string> = JSON.parse(fs.readFileSync("data/alreadyCommands.json", "utf-8"));
|
||
|
||
function cutAfterChar(str: string, char: string) {
|
||
const index = str.indexOf(char);
|
||
|
||
if (index === -1) {
|
||
return "";
|
||
}
|
||
|
||
return str.substring(index + 1);
|
||
}
|
||
|
||
export async function Reply(text: string, reply: string) {
|
||
const req = await fetch(`${config.uwuzu.host}/api/ueuse/create`, {
|
||
method: "POST",
|
||
body: JSON.stringify({
|
||
token: config.uwuzu.apiToken,
|
||
text: text,
|
||
replyid: reply,
|
||
}),
|
||
cache: "no-store",
|
||
});
|
||
|
||
const res = await req.json();
|
||
|
||
return res;
|
||
}
|
||
|
||
function alreadyAdd(data: string) {
|
||
alreadyCommands[alreadyCommands.length] = data;
|
||
|
||
fs.writeFileSync(
|
||
"data/alreadyCommands.json",
|
||
JSON.stringify(alreadyCommands),
|
||
"utf-8",
|
||
);
|
||
}
|
||
|
||
export default async function Commands() {
|
||
const mentionsReq = await fetch(
|
||
`${config.uwuzu.host}/api/ueuse/mentions`, {
|
||
method: "POST",
|
||
body: JSON.stringify({
|
||
token: config.uwuzu.apiToken,
|
||
}),
|
||
cache: "no-store",
|
||
}
|
||
);
|
||
|
||
const mentions: { [key: string]: ueuse } = await mentionsReq.json();
|
||
|
||
console.log("----------------");
|
||
console.log("コマンド処理");
|
||
|
||
for (const key in mentions) {
|
||
if (mentions.hasOwnProperty(key)) {
|
||
const data = mentions[key];
|
||
|
||
// 除外ユーズ
|
||
if (data.text === undefined) {
|
||
break;
|
||
}
|
||
|
||
if (alreadyCommands.indexOf(data.uniqid) !== -1) {
|
||
break;
|
||
}
|
||
|
||
if (
|
||
data.text.charAt(0) === "!" ||
|
||
data.text.charAt(0) === "!" ||
|
||
data.abi === "ignore"
|
||
) {
|
||
break;
|
||
}
|
||
|
||
// コマンド処理
|
||
console.log("--------");
|
||
|
||
const commandName = cutAfterChar(data.text, "/");
|
||
|
||
alreadyAdd(data.uniqid);
|
||
|
||
switch (commandName) {
|
||
case "":
|
||
break;
|
||
case "info":
|
||
Info(data);
|
||
break;
|
||
case "help":
|
||
Help(data);
|
||
break;
|
||
case "legal terms":
|
||
Terms(data);
|
||
break;
|
||
case "legal privacy":
|
||
PrivacyPolicy(data);
|
||
break;
|
||
case "report":
|
||
Report(data);
|
||
break;
|
||
case "follow":
|
||
Follow(data);
|
||
break;
|
||
case "unfollow":
|
||
UnFollow(data);
|
||
break;
|
||
case "weather":
|
||
Weather(data);
|
||
break;
|
||
default:
|
||
const reply = await Reply(`
|
||
不明なコマンドです。
|
||
コマンド実行を除外する場合は1文字目に\`!\`を入れてください。
|
||
`, data.uniqid);
|
||
|
||
console.log("未対応コマンド: ", reply);
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
}
|