noticeUwuzu/scripts/commands/main.ts

146 lines
3.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import * as fs from "fs";
import config from "../../config.js";
import type { ueuse } from "types/types.js";
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 Birthday from "./birthday.js";
import Terms from "./legal/terms.js"
import PrivacyPolicy from "./legal/privacy.js";
// 初期化
if (!fs.existsSync("logs/alreadyCommands.json")) {
fs.writeFileSync(
"logs/alreadyCommands.json",
JSON.stringify(initialFile),
"utf-8",
);
}
// 対応済みユーズ一覧
const alreadyCommands: Array<string> = JSON.parse(fs.readFileSync("logs/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(`https://${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(
"logs/alreadyCommands.json",
JSON.stringify(alreadyCommands),
"utf-8",
);
}
export default async function Commands() {
const mentionsReq = await fetch(
`https://${config.uwuzu.host}/api/ueuse/mentions`, {
method: "POST",
body: JSON.stringify({
token: config.uwuzu.apiToken,
}),
cache: "no-store",
}
);
const mentions: Array<ueuse> = await mentionsReq.json();
console.log("----------------");
console.log("コマンド処理");
for (let i = 0; i < mentions.length; i++) {
const data = mentions[i];
// 除外ユーズ
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;
case "birthday":
Birthday(data);
break;
default:
const reply = await Reply(`
不明なコマンドです。
コマンド実行を除外する場合は1文字目に\`!\`を入れてください。
`, data.uniqid);
console.log("未対応コマンド: ", reply);
break;
}
}
}