diff --git a/src/util/plugin/Plugin.ts b/src/util/plugin/Plugin.ts
index 0fb1732f..6a6f03f4 100644
--- a/src/util/plugin/Plugin.ts
+++ b/src/util/plugin/Plugin.ts
@@ -1,13 +1,22 @@
import EventEmitter from "events";
-import { PluginLoadedEventArgs, TypedEventEmitter } from "@fosscord/util";
+import { PluginLoadedEventArgs, PluginManifest, TypedEventEmitter } from "@fosscord/util";
+import { PluginConfig } from "./PluginConfig";
+import { PreRegisterEventArgs, PreRegisterEventResult, OnRegisterEventArgs } from '.';
+import { PreMessageEventArgs, PreMessageEventResult, OnMessageEventArgs } from '.';
+import { PreLoginEventArgs, PreLoginEventResult, OnLoginEventArgs } from '.';
+import { PreGuildCreateEventArgs, PreGuildCreateEventResult, OnGuildCreateEventArgs } from '.';
+import { PreChannelCreateEventArgs, PreChannelCreateEventResult, OnChannelCreateEventArgs } from '.';
+import { PreTypingEventArgs, PreTypingEventResult, OnTypingEventArgs } from '.';
+import { PreStatusChangeEventArgs, PreStatusChangeEventResult, OnStatusChangeEventArgs } from '.';
-type PluginEvents = {
+
+/*type PluginEvents = {
error: (error: Error | unknown) => void;
loaded: () => void;
-};
+};*/
//this doesnt work, check later:
- //EventEmitter as new () => TypedEventEmitter<PluginEvents>
+//EventEmitter as new () => TypedEventEmitter<PluginEvents>
export class Plugin {
/**
* Path the plugin resides in.
@@ -15,23 +24,137 @@ export class Plugin {
* @type {string}
* @memberof Plugin
*/
- pluginPath: string;
- /**
- *
- *
- * @memberof Plugin
- */
- async initConfig() {
- // insert default config into database?
- console.log("did you forget to implement initConfig?");
- }
+ pluginPath?: string;
+ pluginManifest?: PluginManifest;
/**
*
*
* @param {PluginLoadedEventArgs} args Info about plugin environment
* @memberof Plugin
*/
- onPluginLoaded?(args?: PluginLoadedEventArgs) {
-
+ async onPluginLoaded?(args?: PluginLoadedEventArgs) {
+
}
+
+ //generated
+ /**
+ * RegisterEvent: document me
+ *
+ * @param {OnRegisterEventArgs} args Info about what's going on
+ * @memberof Plugin
+ */
+ async onRegister?(args: OnRegisterEventArgs): Promise<void>;
+
+ /**
+ * RegisterEvent: Executed before changes are announced
+ * document me.
+ *
+ * @param {PreRegisterEventArgs} args Info about what's going on
+ * @return {PreRegisterEventResult} How event should be handled
+ * @memberof Plugin
+ */
+ async onPreRegister?(args: PreRegisterEventArgs): Promise<PreRegisterEventResult>;
+ /**
+ * MessageEvent: document me
+ *
+ * @param {OnMessageEventArgs} args Info about what's going on
+ * @memberof Plugin
+ */
+ async onMessage?(args: OnMessageEventArgs): Promise<void>;
+
+ /**
+ * MessageEvent: Executed before changes are announced
+ * document me.
+ *
+ * @param {PreMessageEventArgs} args Info about what's going on
+ * @return {PreMessageEventResult} How event should be handled
+ * @memberof Plugin
+ */
+ async onPreMessage?(args: PreMessageEventArgs): Promise<PreMessageEventResult>;
+ /**
+ * LoginEvent: document me
+ *
+ * @param {OnLoginEventArgs} args Info about what's going on
+ * @memberof Plugin
+ */
+ async onLogin?(args: OnLoginEventArgs): Promise<void>;
+
+ /**
+ * LoginEvent: Executed before changes are announced
+ * document me.
+ *
+ * @param {PreLoginEventArgs} args Info about what's going on
+ * @return {PreLoginEventResult} How event should be handled
+ * @memberof Plugin
+ */
+ async onPreLogin?(args: PreLoginEventArgs): Promise<PreLoginEventResult>;
+ /**
+ * GuildCreateEvent: document me
+ *
+ * @param {OnGuildCreateEventArgs} args Info about what's going on
+ * @memberof Plugin
+ */
+ async onGuildCreate?(args: OnGuildCreateEventArgs): Promise<void>;
+
+ /**
+ * GuildCreateEvent: Executed before changes are announced
+ * document me.
+ *
+ * @param {PreGuildCreateEventArgs} args Info about what's going on
+ * @return {PreGuildCreateEventResult} How event should be handled
+ * @memberof Plugin
+ */
+ async onPreGuildCreate?(args: PreGuildCreateEventArgs): Promise<PreGuildCreateEventResult>;
+ /**
+ * ChannelCreateEvent: document me
+ *
+ * @param {OnChannelCreateEventArgs} args Info about what's going on
+ * @memberof Plugin
+ */
+ async onChannelCreate?(args: OnChannelCreateEventArgs): Promise<void>;
+
+ /**
+ * ChannelCreateEvent: Executed before changes are announced
+ * document me.
+ *
+ * @param {PreChannelCreateEventArgs} args Info about what's going on
+ * @return {PreChannelCreateEventResult} How event should be handled
+ * @memberof Plugin
+ */
+ async onPreChannelCreate?(args: PreChannelCreateEventArgs): Promise<PreChannelCreateEventResult>;
+ /**
+ * TypingEvent: document me
+ *
+ * @param {OnTypingEventArgs} args Info about what's going on
+ * @memberof Plugin
+ */
+ async onTyping?(args: OnTypingEventArgs): Promise<void>;
+
+ /**
+ * TypingEvent: Executed before changes are announced
+ * document me.
+ *
+ * @param {PreTypingEventArgs} args Info about what's going on
+ * @return {PreTypingEventResult} How event should be handled
+ * @memberof Plugin
+ */
+ async onPreTyping?(args: PreTypingEventArgs): Promise<PreTypingEventResult>;
+ /**
+ * StatusChangeEvent: document me
+ *
+ * @param {OnStatusChangeEventArgs} args Info about what's going on
+ * @memberof Plugin
+ */
+ async onStatusChange?(args: OnStatusChangeEventArgs): Promise<void>;
+
+ /**
+ * StatusChangeEvent: Executed before changes are announced
+ * document me.
+ *
+ * @param {PreStatusChangeEventArgs} args Info about what's going on
+ * @return {PreStatusChangeEventResult} How event should be handled
+ * @memberof Plugin
+ */
+ async onPreStatusChange?(args: PreStatusChangeEventArgs): Promise<PreStatusChangeEventResult>;
+
}
diff --git a/src/util/plugin/PluginConfig.ts b/src/util/plugin/PluginConfig.ts
index c7a7db87..883bca7c 100644
--- a/src/util/plugin/PluginConfig.ts
+++ b/src/util/plugin/PluginConfig.ts
@@ -1,26 +1,26 @@
-import { ConfigEntity } from "../entities/Config";
import fs from "fs";
import { OrmUtils, Environment } from "..";
+import { PluginConfigEntity } from "util/entities/PluginConfig";
// TODO: yaml instead of json
const overridePath = process.env.PLUGIN_CONFIG_PATH ?? "";
let config: any;
-let pairs: ConfigEntity[];
+let pairs: PluginConfigEntity[];
// TODO: use events to inform about config updates
// Config keys are separated with _
-export const Config = {
+export const PluginConfig = {
init: async function init() {
if (config) return config;
- console.log('[Config] Loading configuration...')
- pairs = await ConfigEntity.find();
+ console.log('[PluginConfig] Loading configuration...')
+ pairs = await PluginConfigEntity.find();
config = pairsToConfig(pairs);
//config = (config || {}).merge(new ConfigValue());
//config = OrmUtils.mergeDeep(new ConfigValue(), config)
- if(process.env.CONFIG_PATH)
+ if(process.env.PLUGIN_CONFIG_PATH)
try {
const overrideConfig = JSON.parse(fs.readFileSync(overridePath, { encoding: "utf8" }));
config = overrideConfig.merge(config);
@@ -48,26 +48,32 @@ export const Config = {
function applyConfig(val: any) {
async function apply(obj: any, key = ""): Promise<any> {
- if (typeof obj === "object" && obj !== null)
+ if (typeof obj === "object" && obj !== null && !(obj instanceof Date))
return Promise.all(Object.keys(obj).map((k) => apply(obj[k], key ? `${key}_${k}` : k)));
let pair = pairs.find((x) => x.key === key);
- if (!pair) pair = new ConfigEntity();
+ if (!pair) pair = new PluginConfigEntity();
pair.key = key;
pair.value = obj;
- return pair.save();
+ if(!pair.key || pair.key == null) {
+ console.log(`[PluginConfig] WARN: Empty key`)
+ console.log(pair);
+ if(Environment.isDebug) debugger;
+ }
+ else
+ return pair.save();
}
- if(process.env.CONFIG_PATH) {
+ if(process.env.PLUGIN_CONFIG_PATH) {
if(Environment.isDebug)
- console.log(`Writing config: ${process.env.CONFIG_PATH}`)
+ console.log(`Writing config: ${process.env.PLUGIN_CONFIG_PATH}`)
fs.writeFileSync(overridePath, JSON.stringify(val, null, 4));
}
return apply(val);
}
-function pairsToConfig(pairs: ConfigEntity[]) {
+function pairsToConfig(pairs: PluginConfigEntity[]) {
let value: any = {};
pairs.forEach((p) => {
diff --git a/src/util/plugin/PluginEventHandler.ts b/src/util/plugin/PluginEventHandler.ts
new file mode 100644
index 00000000..d5fc67f4
--- /dev/null
+++ b/src/util/plugin/PluginEventHandler.ts
@@ -0,0 +1,67 @@
+import { PreRegisterEventArgs, OnRegisterEventArgs, PreRegisterEventResult } from './event_types';
+import { PreMessageEventArgs, OnMessageEventArgs, PreMessageEventResult } from './event_types';
+import { PreLoginEventArgs, OnLoginEventArgs, PreLoginEventResult } from './event_types';
+import { PreGuildCreateEventArgs, OnGuildCreateEventArgs, PreGuildCreateEventResult } from './event_types';
+import { PreChannelCreateEventArgs, OnChannelCreateEventArgs, PreChannelCreateEventResult } from './event_types';
+import { PreTypingEventArgs, OnTypingEventArgs, PreTypingEventResult } from './event_types';
+import { PreStatusChangeEventArgs, OnStatusChangeEventArgs, PreStatusChangeEventResult } from './event_types';
+import { PluginStore } from ".";
+
+export class PluginEventHandler {
+ public static async preRegisterEvent(args: PreRegisterEventArgs): Promise<PreRegisterEventResult[]> {
+ return (await Promise.all(PluginStore.plugins.filter(x=>x.onPreRegister).map(x=>x.onPreRegister && x.onPreRegister(args)))).filter(x=>x) as PreRegisterEventResult[];
+ }
+
+ public static async onRegisterEvent(args: OnRegisterEventArgs): Promise<void> {
+ await Promise.all(PluginStore.plugins.filter(x=>x.onRegister).map(x=>x.onRegister && x.onRegister(args)));
+ }
+
+ public static async preMessageEvent(args: PreMessageEventArgs): Promise<PreMessageEventResult[]> {
+ return (await Promise.all(PluginStore.plugins.filter(x=>x.onPreMessage).map(x=>x.onPreMessage && x.onPreMessage(args)))).filter(x=>x) as PreMessageEventResult[];
+ }
+
+ public static async onMessageEvent(args: OnMessageEventArgs): Promise<void> {
+ await Promise.all(PluginStore.plugins.filter(x=>x.onMessage).map(x=>x.onMessage && x.onMessage(args)));
+ }
+
+ public static async preLoginEvent(args: PreLoginEventArgs): Promise<PreLoginEventResult[]> {
+ return (await Promise.all(PluginStore.plugins.filter(x=>x.onPreLogin).map(x=>x.onPreLogin && x.onPreLogin(args)))).filter(x=>x) as PreLoginEventResult[];
+ }
+
+ public static async onLoginEvent(args: OnLoginEventArgs): Promise<void> {
+ await Promise.all(PluginStore.plugins.filter(x=>x.onLogin).map(x=>x.onLogin && x.onLogin(args)));
+ }
+
+ public static async preGuildCreateEvent(args: PreGuildCreateEventArgs): Promise<PreGuildCreateEventResult[]> {
+ return (await Promise.all(PluginStore.plugins.filter(x=>x.onPreGuildCreate).map(x=>x.onPreGuildCreate && x.onPreGuildCreate(args)))).filter(x=>x) as PreGuildCreateEventResult[];
+ }
+
+ public static async onGuildCreateEvent(args: OnGuildCreateEventArgs): Promise<void> {
+ await Promise.all(PluginStore.plugins.filter(x=>x.onGuildCreate).map(x=>x.onGuildCreate && x.onGuildCreate(args)));
+ }
+
+ public static async preChannelCreateEvent(args: PreChannelCreateEventArgs): Promise<PreChannelCreateEventResult[]> {
+ return (await Promise.all(PluginStore.plugins.filter(x=>x.onPreChannelCreate).map(x=>x.onPreChannelCreate && x.onPreChannelCreate(args)))).filter(x=>x) as PreChannelCreateEventResult[];
+ }
+
+ public static async onChannelCreateEvent(args: OnChannelCreateEventArgs): Promise<void> {
+ await Promise.all(PluginStore.plugins.filter(x=>x.onChannelCreate).map(x=>x.onChannelCreate && x.onChannelCreate(args)));
+ }
+
+ public static async preTypingEvent(args: PreTypingEventArgs): Promise<PreTypingEventResult[]> {
+ return (await Promise.all(PluginStore.plugins.filter(x=>x.onPreTyping).map(x=>x.onPreTyping && x.onPreTyping(args)))).filter(x=>x) as PreTypingEventResult[];
+ }
+
+ public static async onTypingEvent(args: OnTypingEventArgs): Promise<void> {
+ await Promise.all(PluginStore.plugins.filter(x=>x.onTyping).map(x=>x.onTyping && x.onTyping(args)));
+ }
+
+ public static async preStatusChangeEvent(args: PreStatusChangeEventArgs): Promise<PreStatusChangeEventResult[]> {
+ return (await Promise.all(PluginStore.plugins.filter(x=>x.onPreStatusChange).map(x=>x.onPreStatusChange && x.onPreStatusChange(args)))).filter(x=>x) as PreStatusChangeEventResult[];
+ }
+
+ public static async onStatusChangeEvent(args: OnStatusChangeEventArgs): Promise<void> {
+ await Promise.all(PluginStore.plugins.filter(x=>x.onStatusChange).map(x=>x.onStatusChange && x.onStatusChange(args)));
+ }
+
+}
diff --git a/src/util/plugin/PluginLoader.ts b/src/util/plugin/PluginLoader.ts
index 8c140d29..4dc0129a 100644
--- a/src/util/plugin/PluginLoader.ts
+++ b/src/util/plugin/PluginLoader.ts
@@ -1,14 +1,17 @@
import path from "path";
import fs from "fs";
-import { Plugin, PluginLoadedEventArgs, PluginManifest } from "./";
+import { Plugin, PluginLoadedEventArgs, PluginManifest, PluginStore } from "./";
import { PluginIndex } from "plugins/PluginIndex";
+import { PluginConfig } from "./PluginConfig";
+import { OrmUtils, PluginConfigEntity } from "..";
const root = process.env.PLUGIN_LOCATION || "dist/plugins";
let pluginsLoaded = false;
export class PluginLoader {
- public static plugins: Plugin[] = [];
- public static loadPlugins() {
+ public static async loadPlugins() {
+ if(pluginsLoaded) return;
+ PluginConfig.init();
console.log(`Plugin root directory: ${path.resolve(root)}`);
const dirs = fs.readdirSync(root).filter((x) => {
try {
@@ -18,24 +21,45 @@ export class PluginLoader {
return false;
}
});
- console.log(dirs);
- PluginIndex.forEach((x: any)=>{
- console.log(x.onPluginLoaded)
- })
+ //console.log(dirs);
+ PluginIndex.forEach((x: any) => {
+ //console.log(x.onPluginLoaded)
+ });
dirs.forEach(async (x) => {
let modPath = path.resolve(path.join(root, x));
- console.log(`Trying to load plugin: ${modPath}`);
+ //console.log(`Trying to load plugin: ${modPath}`);
const manifest = require(path.join(modPath, "plugin.json")) as PluginManifest;
console.log(
`Plugin info: ${manifest.name} (${manifest.id}), written by ${manifest.authors}, available at ${manifest.repository}`
);
- const module_ = PluginIndex["example-plugin"];
+ const module_ = PluginIndex[manifest.id];
module_.pluginPath = modPath;
- if(module_.onPluginLoaded) module_.onPluginLoaded({} as PluginLoadedEventArgs);
- this.plugins.push(module_);
+ module_.pluginManifest = manifest;
+ Object.freeze(module_.pluginPath);
+ Object.freeze(module_.pluginManifest);
+
+ if(module_.onPluginLoaded) await module_.onPluginLoaded({} as PluginLoadedEventArgs);
+ PluginStore.plugins.push(module_);
});
- console.log(`Done loading ${this.plugins.length} plugins!`)
+ console.log(`Done loading ${PluginStore.plugins.length} plugins!`);
+ }
+
+ public static getPluginConfig(id: string, defaults?: any): any {
+ let cfg = PluginConfig.get()[id];
+ if(defaults) {
+ if(cfg)
+ cfg = OrmUtils.mergeDeep(defaults, cfg);
+ else
+ cfg = defaults;
+ this.setPluginConfig(id, cfg);
+ }
+ if(!cfg) console.log(`[PluginConfig/WARN] Getting plugin settings for '${id}' returned null! (Did you forget to add settings?)`);
+ return cfg;
+ }
+ public static async setPluginConfig(id: string, config: Partial<any>): Promise<void> {
+ if(!config) console.log(`[PluginConfig/WARN] ${id} tried to set config=null!`);
+ await PluginConfig.set({ [id]: OrmUtils.mergeDeep(PluginLoader.getPluginConfig(id) || {}, config) });
}
}
diff --git a/src/util/plugin/PluginStore.ts b/src/util/plugin/PluginStore.ts
new file mode 100644
index 00000000..60d7f7b7
--- /dev/null
+++ b/src/util/plugin/PluginStore.ts
@@ -0,0 +1,12 @@
+import path from "path";
+import fs from "fs";
+import { Plugin, PluginLoadedEventArgs, PluginManifest } from "./";
+import { PluginIndex } from "plugins/PluginIndex";
+import { PluginConfig } from "./PluginConfig";
+import { OrmUtils, PluginConfigEntity } from "..";
+
+const root = process.env.PLUGIN_LOCATION || "dist/plugins";
+
+export class PluginStore {
+ public static plugins: Plugin[] = [];
+}
diff --git a/src/util/plugin/event_types/ChannelCreateEventArgs.ts b/src/util/plugin/event_types/ChannelCreateEventArgs.ts
new file mode 100644
index 00000000..87fa3691
--- /dev/null
+++ b/src/util/plugin/event_types/ChannelCreateEventArgs.ts
@@ -0,0 +1,17 @@
+import { Channel, Guild, User } from "util/entities";
+import { EventResult } from ".";
+
+export interface PreChannelCreateEventArgs {
+ channel: Channel,
+ guild: Guild,
+ user: User
+}
+export interface PreChannelCreateEventResult extends EventResult {
+ channel: Partial<Channel>
+}
+
+export interface OnChannelCreateEventArgs {
+ channel: Channel,
+ guild: Guild,
+ user: User
+}
diff --git a/src/util/plugin/event_types/GuildCreateEventArgs.ts b/src/util/plugin/event_types/GuildCreateEventArgs.ts
new file mode 100644
index 00000000..7724b4f3
--- /dev/null
+++ b/src/util/plugin/event_types/GuildCreateEventArgs.ts
@@ -0,0 +1,15 @@
+import { Guild, User } from "util/entities";
+import { EventResult } from ".";
+
+export interface PreGuildCreateEventArgs {
+ user: User,
+ guild: Guild
+}
+export interface PreGuildCreateEventResult extends EventResult {
+ guild: Partial<Guild>
+}
+
+export interface OnGuildCreateEventArgs {
+ user: User,
+ guild: Guild
+}
diff --git a/src/util/plugin/event_types/LoginEventArgs.ts b/src/util/plugin/event_types/LoginEventArgs.ts
new file mode 100644
index 00000000..8f80b69f
--- /dev/null
+++ b/src/util/plugin/event_types/LoginEventArgs.ts
@@ -0,0 +1,15 @@
+import { User } from "util/entities";
+import { EventResult } from ".";
+
+export interface PreLoginEventArgs {
+ ip: String,
+ user: User
+}
+export interface PreLoginEventResult extends EventResult {
+
+}
+
+export interface OnLoginEventArgs {
+ ip: String,
+ user: User
+}
diff --git a/src/util/plugin/event_types/MessageEventArgs.ts b/src/util/plugin/event_types/MessageEventArgs.ts
new file mode 100644
index 00000000..ab276429
--- /dev/null
+++ b/src/util/plugin/event_types/MessageEventArgs.ts
@@ -0,0 +1,15 @@
+import { Message, User } from "util/entities";
+import { EventResult } from ".";
+
+export interface PreMessageEventArgs {
+ user: User,
+ message: Message;
+}
+export interface PreMessageEventResult extends EventResult {
+ message: Partial<Message>
+}
+
+export interface OnMessageEventArgs {
+ user: User,
+ message: Message
+}
diff --git a/src/util/plugin/plugin_data_objects/PluginLoadedEventArgs.ts b/src/util/plugin/event_types/PluginLoadedEventArgs.ts
index 58829f15..58829f15 100644
--- a/src/util/plugin/plugin_data_objects/PluginLoadedEventArgs.ts
+++ b/src/util/plugin/event_types/PluginLoadedEventArgs.ts
diff --git a/src/util/plugin/event_types/RegisterEventArgs.ts b/src/util/plugin/event_types/RegisterEventArgs.ts
new file mode 100644
index 00000000..d36d7e64
--- /dev/null
+++ b/src/util/plugin/event_types/RegisterEventArgs.ts
@@ -0,0 +1,17 @@
+import { User } from "util/entities";
+import { EventResult } from ".";
+
+export interface PreRegisterEventArgs {
+ age: any,
+ user: User,
+ ip: String
+}
+export interface PreRegisterEventResult extends EventResult {
+ user: Partial<User>
+}
+
+export interface OnRegisterEventArgs {
+ age: any,
+ user: User,
+ ip: String
+}
diff --git a/src/util/plugin/event_types/StatusChangeEventArgs.ts b/src/util/plugin/event_types/StatusChangeEventArgs.ts
new file mode 100644
index 00000000..c1a67112
--- /dev/null
+++ b/src/util/plugin/event_types/StatusChangeEventArgs.ts
@@ -0,0 +1,16 @@
+import { User } from "util/entities";
+import { Presence } from "util/interfaces";
+import { EventResult } from ".";
+
+export interface PreStatusChangeEventArgs {
+ user: User,
+ presence: Presence
+}
+export interface PreStatusChangeEventResult extends EventResult {
+ presence: Partial<Presence>
+}
+
+export interface OnStatusChangeEventArgs {
+ user: User,
+ presence: Presence
+}
diff --git a/src/util/plugin/event_types/TypingEventArgs.ts b/src/util/plugin/event_types/TypingEventArgs.ts
new file mode 100644
index 00000000..3ac59b41
--- /dev/null
+++ b/src/util/plugin/event_types/TypingEventArgs.ts
@@ -0,0 +1,17 @@
+import { Channel, Guild, User } from "util/entities";
+import { EventResult } from ".";
+
+export interface PreTypingEventArgs {
+ channel: Channel,
+ guild: Guild,
+ user: User
+}
+export interface PreTypingEventResult extends EventResult {
+
+}
+
+export interface OnTypingEventArgs {
+ channel: Channel,
+ guild: Guild,
+ user: User
+}
diff --git a/src/util/plugin/event_types/_gen.sh b/src/util/plugin/event_types/_gen.sh
new file mode 100755
index 00000000..9f761d1e
--- /dev/null
+++ b/src/util/plugin/event_types/_gen.sh
@@ -0,0 +1,74 @@
+#!/bin/sh
+rm -f ../plugin.eventfuncs.generated
+rm -f ../plugin.imports.generated
+while read event
+do
+ echo Making event $event...
+ (
+ echo 'import { EventResult } from ".";'
+ echo ''
+ echo "export interface Pre${event}EventArgs {"
+ echo ' '
+ echo '}'
+ echo "export interface Pre${event}EventResult extends EventResult {"
+ echo ' '
+ echo '}'
+ echo ''
+ echo "export interface On${event}EventArgs {"
+ echo ' '
+ echo '}'
+ ) > ${event}EventArgs.ts.generated
+ (
+ echo " public static async pre${event}Event(args: Pre${event}EventArgs): Promise<Pre${event}EventResult[]> {"
+ echo " return (await Promise.all(PluginStore.plugins.filter(x=>x.onPre${event}).map(x=>x.onPre${event} && x.onPre${event}(args)))).filter(x=>x) as Pre${event}EventResult[];"
+ echo ' }'
+ echo ' '
+ echo " public static async on${event}Event(args: On${event}EventArgs): Promise<void> {"
+ echo " await Promise.all(PluginStore.plugins.filter(x=>x.on${event}).map(x=>x.on${event} && x.on${event}(args)));"
+ echo ' }'
+ echo ' '
+ ) >> ../PluginEventHandler.ts.3
+ (
+ echo " /**"
+ echo " * ${event}Event: document me"
+ echo " *"
+ echo " * @param {On${event}EventArgs} args Info about what's going on"
+ echo " * @memberof Plugin"
+ echo " */"
+ echo " async on${event}?(args: On${event}EventArgs): Promise<void>;"
+ echo ' '
+ echo " /**"
+ echo " * ${event}Event: Executed before changes are announced"
+ echo " * document me."
+ echo " *"
+ echo " * @param {Pre${event}EventArgs} args Info about what's going on"
+ echo " * @return {Pre${event}EventResult} How event should be handled"
+ echo " * @memberof Plugin"
+ echo " */"
+ echo " async onPre${event}?(args: Pre${event}EventArgs): Promise<Pre${event}EventResult>;"
+ ) >> ../plugin.eventfuncs.generated
+
+ echo "import { Pre${event}EventArgs, On${event}EventArgs, Pre${event}EventResult } from './event_types';" >> ../PluginEventHandler.ts.1
+ echo "import { Pre${event}EventArgs, Pre${event}EventResult, On${event}EventArgs } from '.';" >> ../plugin.imports.generated
+ cmp --silent "${event}EventArgs.ts" "${event}EventArgs.ts.generated" && rm -f "${event}EventArgs.ts.generated"
+done < _pdo
+
+echo 'Building PluginEventHandler...'
+
+rm -f ../PluginEventHandler.ts.generated
+(
+ echo 'import { PluginStore } from ".";'
+ echo ''
+ echo 'export class PluginEventHandler {'
+) > ../PluginEventHandler.ts.2
+echo '}' > ../PluginEventHandler.ts.4
+for i in {1..4}
+do
+ cat "../PluginEventHandler.ts.$i" >> ../PluginEventHandler.ts.generated
+ rm -f ../PluginEventHandler.ts.$i
+done
+cmp --silent ../PluginEventHandler.ts ../PluginEventHandler.ts.generated && rm -f ../PluginEventHandler.ts.generated
+
+echo 'Rebuilding indexes...'
+node ../../../../scripts/gen_index.js .. --recursive
+echo 'Done!'
\ No newline at end of file
diff --git a/src/util/plugin/plugin_data_objects/_pdo b/src/util/plugin/event_types/_pdo
index fa207f77..f6127174 100644
--- a/src/util/plugin/plugin_data_objects/_pdo
+++ b/src/util/plugin/event_types/_pdo
@@ -4,4 +4,5 @@ Login
GuildCreate
ChannelCreate
Typing
-StatusChange
\ No newline at end of file
+StatusChange
+UserProfileUpdate
\ No newline at end of file
diff --git a/src/util/plugin/plugin_data_objects/_todo.txt b/src/util/plugin/event_types/_todo.txt
index a6a78c7e..a6a78c7e 100644
--- a/src/util/plugin/plugin_data_objects/_todo.txt
+++ b/src/util/plugin/event_types/_todo.txt
diff --git a/src/util/plugin/event_types/base/EventResult.ts b/src/util/plugin/event_types/base/EventResult.ts
new file mode 100644
index 00000000..f18837cd
--- /dev/null
+++ b/src/util/plugin/event_types/base/EventResult.ts
@@ -0,0 +1,4 @@
+export interface EventResult {
+ cancel?: boolean;
+ blockReason?: string;
+}
\ No newline at end of file
diff --git a/src/util/plugin/event_types/base/index.ts b/src/util/plugin/event_types/base/index.ts
new file mode 100644
index 00000000..fd0dbd03
--- /dev/null
+++ b/src/util/plugin/event_types/base/index.ts
@@ -0,0 +1 @@
+export * from "./EventResult";
diff --git a/src/util/plugin/plugin_data_objects/index.ts b/src/util/plugin/event_types/index.ts
index c75d43f9..4a585dc0 100644
--- a/src/util/plugin/plugin_data_objects/index.ts
+++ b/src/util/plugin/event_types/index.ts
@@ -4,4 +4,6 @@ export * from "./LoginEventArgs";
export * from "./MessageEventArgs";
export * from "./PluginLoadedEventArgs";
export * from "./RegisterEventArgs";
+export * from "./StatusChangeEventArgs";
export * from "./TypingEventArgs";
+export * from "./base/index";
diff --git a/src/util/plugin/index.ts b/src/util/plugin/index.ts
index 5974a065..7a297981 100644
--- a/src/util/plugin/index.ts
+++ b/src/util/plugin/index.ts
@@ -1,4 +1,7 @@
export * from "./Plugin";
+export * from "./PluginConfig";
+export * from "./PluginEventHandler";
export * from "./PluginLoader";
export * from "./PluginManifest";
-export * from "./plugin_data_objects/index";
+export * from "./PluginStore";
+export * from "./event_types/index";
diff --git a/src/util/plugin/plugin_data_objects/ChannelCreateEventArgs.ts b/src/util/plugin/plugin_data_objects/ChannelCreateEventArgs.ts
deleted file mode 100644
index ce7dec87..00000000
--- a/src/util/plugin/plugin_data_objects/ChannelCreateEventArgs.ts
+++ /dev/null
@@ -1,7 +0,0 @@
-export interface PreChannelCreateEventArgs {
-
-}
-
-export interface OnChannelCreateEventArgs {
-
-}
diff --git a/src/util/plugin/plugin_data_objects/GuildCreateEventArgs.ts b/src/util/plugin/plugin_data_objects/GuildCreateEventArgs.ts
deleted file mode 100644
index e10e675a..00000000
--- a/src/util/plugin/plugin_data_objects/GuildCreateEventArgs.ts
+++ /dev/null
@@ -1,7 +0,0 @@
-export interface PreGuildCreateEventArgs {
-
-}
-
-export interface OnGuildCreateEventArgs {
-
-}
diff --git a/src/util/plugin/plugin_data_objects/LoginEventArgs.ts b/src/util/plugin/plugin_data_objects/LoginEventArgs.ts
deleted file mode 100644
index 391b852e..00000000
--- a/src/util/plugin/plugin_data_objects/LoginEventArgs.ts
+++ /dev/null
@@ -1,7 +0,0 @@
-export interface PreLoginEventArgs {
-
-}
-
-export interface OnLoginEventArgs {
-
-}
diff --git a/src/util/plugin/plugin_data_objects/MessageEventArgs.ts b/src/util/plugin/plugin_data_objects/MessageEventArgs.ts
deleted file mode 100644
index 0a3498c2..00000000
--- a/src/util/plugin/plugin_data_objects/MessageEventArgs.ts
+++ /dev/null
@@ -1,7 +0,0 @@
-export interface PreMessageEventArgs {
-
-}
-
-export interface OnMessageEventArgs {
-
-}
diff --git a/src/util/plugin/plugin_data_objects/RegisterEventArgs.ts b/src/util/plugin/plugin_data_objects/RegisterEventArgs.ts
deleted file mode 100644
index 7f7c0c76..00000000
--- a/src/util/plugin/plugin_data_objects/RegisterEventArgs.ts
+++ /dev/null
@@ -1,7 +0,0 @@
-export interface PreRegisterEventArgs {
-
-}
-
-export interface OnRegisterEventArgs {
-
-}
diff --git a/src/util/plugin/plugin_data_objects/TypingEventArgs.ts b/src/util/plugin/plugin_data_objects/TypingEventArgs.ts
deleted file mode 100644
index f6660692..00000000
--- a/src/util/plugin/plugin_data_objects/TypingEventArgs.ts
+++ /dev/null
@@ -1,7 +0,0 @@
-export interface PreTypingEventArgs {
-
-}
-
-export interface OnTypingEventArgs {
-
-}
diff --git a/src/util/plugin/plugin_data_objects/_gen.sh b/src/util/plugin/plugin_data_objects/_gen.sh
deleted file mode 100755
index 9fbd1749..00000000
--- a/src/util/plugin/plugin_data_objects/_gen.sh
+++ /dev/null
@@ -1,21 +0,0 @@
-#!/bin/sh
-while read event
-do
- if [ ! -f "${event}EventArgs.ts" ]
- then
- echo Making event $event...
- (
- echo "export interface Pre${event}EventArgs {"
- echo ' '
- echo '}'
- echo ''
- echo "export interface On${event}EventArgs {"
- echo ' '
- echo '}'
- ) > ${event}EventArgs.ts
- fi
-done < _pdo
-
-echo ''
-
-node ../../../../scripts/gen_index.js .. --recursive
\ No newline at end of file
|