Skip to content

Commit

Permalink
Merge pull request #183 from private-yusuke/instantiate-modules-with-…
Browse files Browse the repository at this point in the history
…an-instance-of-Ai

Instantiate modules with an instance of `Ai`
  • Loading branch information
private-yusuke authored Jun 27, 2023
2 parents c733d89 + d2303fc commit 164a8c4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "botot2",
"version": "4.0.0",
"version": "4.0.1",
"description": "A chatbot for Misskey with Markov chain",
"main": "index.js",
"scripts": {
Expand Down
30 changes: 17 additions & 13 deletions src/ai.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import config from "./config";
import IModule from "./module";
import Modulez from "./modules";
import * as WebSocket from "ws";
import { User, Reaction, generateUserId, api } from "./misskey";
import * as moment from "moment";
Expand All @@ -10,32 +11,35 @@ import { assertProperty } from "./util/assert-property";

export default class Ai {
public account: User;
public loadedModules: IModule[] = [];
private connection: any;
modules: IModule[] = [];
private isInterrupted: boolean = false;
meta: any;

constructor(account: User, modules: IModule[]) {
constructor(account: User) {
this.account = account;
this.modules = modules;

this.init();
}

private moduleCandidates() {
return Modulez.map((M) => new M(this))
.filter((m) => config.modules.includes(m.name))
.sort((a, b) => b.priority - a.priority);
}

private init() {
let loadedModules: IModule[] = [];
for (let m of this.modules) {
for (let m of this.moduleCandidates()) {
try {
m.install();
loadedModules.push(m);
this.loadedModules.push(m);
} catch (e) {
console.error(`An error has occured while loading module "${m.name}"`);
console.error(e);
}
}
this.modules = loadedModules;
console.info("loaded modules:");
this.modules.forEach((m) => console.log(`${m.priority}: ${m.name}`));
this.loadedModules.forEach((m) => console.log(`${m.priority}: ${m.name}`));

this.initConnection();
console.log({
Expand Down Expand Up @@ -165,7 +169,7 @@ export default class Ai {
}
if (body.user.isBot) return;

this.modules.filter(assertProperty("onNote")).forEach((m) => {
this.loadedModules.filter(assertProperty("onNote")).forEach((m) => {
return m.onNote(body);
});
}
Expand Down Expand Up @@ -194,7 +198,7 @@ export default class Ai {
console.log(
`!${msg.user.name}(@${generateUserId(msg.user)}): ${msg.text}`
);
let funcs = this.modules.filter(assertProperty("onCommand"));
let funcs = this.loadedModules.filter(assertProperty("onCommand"));
let done = false;
for (let i = 0; i < funcs.length; i++) {
if (done) break;
Expand All @@ -204,7 +208,7 @@ export default class Ai {
if (!done) msg.reply("command not found");
} else {
let res: ReturnType<NonNullable<IModule["onMention"]>>;
this.modules.filter(assertProperty("onMention")).some((m) => {
this.loadedModules.filter(assertProperty("onMention")).some((m) => {
res = m.onMention(msg);
return res === true || typeof res === "object";
});
Expand All @@ -216,15 +220,15 @@ export default class Ai {
}

private onFollowed(user: User) {
this.modules.filter(assertProperty("onFollowed")).forEach((m) => {
this.loadedModules.filter(assertProperty("onFollowed")).forEach((m) => {
return m.onFollowed(user);
});
}

async onInterrupt() {
this.isInterrupted = true;
this.connection.close();
this.modules.filter(assertProperty("onInterrupted")).forEach((m) => {
this.loadedModules.filter(assertProperty("onInterrupted")).forEach((m) => {
m.onInterrupted();
});
process.exit(0);
Expand Down
10 changes: 1 addition & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,8 @@ async function main() {
console.log(`I am ${me.name}(@${me.username})!`);
console.log(`Version: ${config.version}(${config.revision})`);
me.host = config.host;
const modules: IModule[] = [];
Modulez.forEach((M) => {
const m = new M(ai);
if (config.modules.indexOf(m.name) >= 0) modules.push(m);
});
modules.sort((a, b) => {
return b.priority - a.priority;
});

ai = new Ai(me, modules);
ai = new Ai(me);
}

process.on("SIGINT", async () => {
Expand Down
8 changes: 5 additions & 3 deletions src/modules/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ export default class AdminModule implements IModule {
public async onCommand(msg: MessageLike, cmd: string[]): Promise<boolean> {
if (cmd[0] == "info") {
let res = `\`\`\`
Modules: ${this.ai.modules.map((i) => `${i.name}(${i.priority})`).join(", ")}
Modules: ${this.ai.loadedModules
.map((i) => `${i.name}(${i.priority})`)
.join(", ")}
Uptime: ${this.getUptime()}
${process.title} ${process.version} ${process.arch} ${process.platform}
Version: ${config.version}(${config.revision})
`;
res += this.ai.modules
res += this.ai.loadedModules
.filter(assertProperty("info"))
.map((i) => i.info())
.join("\n");
Expand All @@ -51,7 +53,7 @@ Version: ${config.version}(${config.revision})
return true;
} else if (cmd[0] == "help") {
let res = "```\n";
this.ai.modules.forEach((v) => {
this.ai.loadedModules.forEach((v) => {
if (v.commands) {
v.commands.forEach((c) => {
if (c.desc) res += `/${c.name}: ${c.desc}\n`;
Expand Down

0 comments on commit 164a8c4

Please sign in to comment.