zappyzep/src/plugins/BotControl.ts

72 lines
1.9 KiB
TypeScript
Raw Normal View History

2018-07-01 03:35:51 +03:00
import { decorators as d, GlobalPlugin } from "knub";
import child_process from "child_process";
import { GuildChannel, Message, TextChannel } from "eris";
import { errorMessage, sleep, successMessage } from "../utils";
let activeReload: [string, string] = null;
2018-07-01 03:35:51 +03:00
/**
* A global plugin that allows bot owners to control the bot
*/
export class BotControlPlugin extends GlobalPlugin {
public static pluginName = 'bot_control';
2018-07-01 03:35:51 +03:00
getDefaultOptions() {
return {
config: {
owners: []
}
};
}
async onLoad() {
if (activeReload) {
const [guildId, channelId] = activeReload;
activeReload = null;
const guild = this.bot.guilds.get(guildId);
if (guild) {
const channel = guild.channels.get(channelId);
if (channel instanceof TextChannel) {
channel.createMessage(successMessage("Global plugins reloaded!"));
}
}
}
}
2018-07-01 03:35:51 +03:00
isOwner(userId) {
return this.configValue("owners").includes(userId);
}
@d.command("bot_full_update")
async fullUpdateCmd(msg: Message) {
2018-07-01 03:35:51 +03:00
if (!this.isOwner(msg.author.id)) return;
const updateCmd = this.configValue("update_cmd");
if (!updateCmd) {
msg.channel.createMessage(errorMessage("Update command not specified!"));
2018-07-01 03:35:51 +03:00
return;
}
msg.channel.createMessage("Updating...");
const updater = child_process.exec(updateCmd, { cwd: process.cwd() });
updater.stderr.on("data", data => {
// tslint:disable-next-line
console.error(data);
});
}
@d.command("bot_reload_global_plugins")
async reloadGlobalPluginsCmd(msg: Message) {
2018-07-01 03:35:51 +03:00
if (!this.isOwner(msg.author.id)) return;
if (activeReload) return;
if (msg.channel) {
activeReload = [(msg.channel as GuildChannel).guild.id, msg.channel.id];
await msg.channel.createMessage("Reloading global plugins...");
}
2018-07-01 03:35:51 +03:00
this.knub.reloadAllGlobalPlugins();
2018-07-01 03:35:51 +03:00
}
}