mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-03-18 15:00:00 +00:00
BotControl: more robust owner checking, use GlobalZeppelinPlugin, add show_plugin_config command
This commit is contained in:
parent
a188b0433c
commit
e0bef49a84
1 changed files with 70 additions and 22 deletions
|
@ -1,32 +1,61 @@
|
||||||
import { decorators as d, GlobalPlugin, IPluginOptions } from "knub";
|
import { decorators as d, IPluginOptions } from "knub";
|
||||||
import child_process from "child_process";
|
import child_process from "child_process";
|
||||||
import { GuildChannel, Message, TextChannel } from "eris";
|
import { GuildChannel, Message, TextChannel } from "eris";
|
||||||
import { createChunkedMessage, errorMessage, noop, sorter, successMessage } from "../utils";
|
import moment from "moment-timezone";
|
||||||
|
import { createChunkedMessage, errorMessage, noop, sorter, successMessage, tNullable } from "../utils";
|
||||||
import { ReactionRolesPlugin } from "./ReactionRoles";
|
import { ReactionRolesPlugin } from "./ReactionRoles";
|
||||||
|
import { ZeppelinPlugin } from "./ZeppelinPlugin";
|
||||||
|
import { GuildArchives } from "../data/GuildArchives";
|
||||||
|
import { GlobalZeppelinPlugin } from "./GlobalZeppelinPlugin";
|
||||||
|
import * as t from "io-ts";
|
||||||
|
|
||||||
let activeReload: [string, string] = null;
|
let activeReload: [string, string] = null;
|
||||||
|
|
||||||
interface IBotControlPluginConfig {
|
const ConfigSchema = t.type({
|
||||||
owners: string[];
|
can_use: t.boolean,
|
||||||
update_cmd: string;
|
owners: t.array(t.string),
|
||||||
}
|
update_cmd: tNullable(t.string),
|
||||||
|
});
|
||||||
|
type TConfigSchema = t.TypeOf<typeof ConfigSchema>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A global plugin that allows bot owners to control the bot
|
* A global plugin that allows bot owners to control the bot
|
||||||
*/
|
*/
|
||||||
export class BotControlPlugin extends GlobalPlugin<IBotControlPluginConfig> {
|
export class BotControlPlugin extends GlobalZeppelinPlugin<TConfigSchema> {
|
||||||
public static pluginName = "bot_control";
|
public static pluginName = "bot_control";
|
||||||
|
protected static configSchema = ConfigSchema;
|
||||||
|
|
||||||
getDefaultOptions(): IPluginOptions<IBotControlPluginConfig> {
|
protected archives: GuildArchives;
|
||||||
|
|
||||||
|
protected static getStaticDefaultOptions() {
|
||||||
return {
|
return {
|
||||||
config: {
|
config: {
|
||||||
|
can_use: false,
|
||||||
owners: [],
|
owners: [],
|
||||||
update_cmd: null,
|
update_cmd: null,
|
||||||
},
|
},
|
||||||
|
overrides: [
|
||||||
|
{
|
||||||
|
level: ">=100",
|
||||||
|
config: {
|
||||||
|
can_use: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected getMemberLevel(member) {
|
||||||
|
if (this.getConfig().owners.includes(member.id)) {
|
||||||
|
return 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
async onLoad() {
|
async onLoad() {
|
||||||
|
this.archives = new GuildArchives(0);
|
||||||
|
|
||||||
if (activeReload) {
|
if (activeReload) {
|
||||||
const [guildId, channelId] = activeReload;
|
const [guildId, channelId] = activeReload;
|
||||||
activeReload = null;
|
activeReload = null;
|
||||||
|
@ -46,9 +75,8 @@ export class BotControlPlugin extends GlobalPlugin<IBotControlPluginConfig> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@d.command("bot_full_update")
|
@d.command("bot_full_update")
|
||||||
|
@d.permission("can_use")
|
||||||
async fullUpdateCmd(msg: Message) {
|
async fullUpdateCmd(msg: Message) {
|
||||||
if (!this.isOwner(msg.author.id)) return;
|
|
||||||
|
|
||||||
const updateCmd = this.getConfig().update_cmd;
|
const updateCmd = this.getConfig().update_cmd;
|
||||||
if (!updateCmd) {
|
if (!updateCmd) {
|
||||||
msg.channel.createMessage(errorMessage("Update command not specified!"));
|
msg.channel.createMessage(errorMessage("Update command not specified!"));
|
||||||
|
@ -64,8 +92,8 @@ export class BotControlPlugin extends GlobalPlugin<IBotControlPluginConfig> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@d.command("bot_reload_global_plugins")
|
@d.command("bot_reload_global_plugins")
|
||||||
|
@d.permission("can_use")
|
||||||
async reloadGlobalPluginsCmd(msg: Message) {
|
async reloadGlobalPluginsCmd(msg: Message) {
|
||||||
if (!this.isOwner(msg.author.id)) return;
|
|
||||||
if (activeReload) return;
|
if (activeReload) return;
|
||||||
|
|
||||||
if (msg.channel) {
|
if (msg.channel) {
|
||||||
|
@ -77,8 +105,8 @@ export class BotControlPlugin extends GlobalPlugin<IBotControlPluginConfig> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@d.command("perf")
|
@d.command("perf")
|
||||||
|
@d.permission("can_use")
|
||||||
async perfCmd(msg: Message) {
|
async perfCmd(msg: Message) {
|
||||||
if (!this.isOwner(msg.author.id)) return;
|
|
||||||
const perfItems = this.knub.getPerformanceDebugItems();
|
const perfItems = this.knub.getPerformanceDebugItems();
|
||||||
|
|
||||||
if (perfItems.length) {
|
if (perfItems.length) {
|
||||||
|
@ -90,9 +118,8 @@ export class BotControlPlugin extends GlobalPlugin<IBotControlPluginConfig> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@d.command("refresh_reaction_roles_globally")
|
@d.command("refresh_reaction_roles_globally")
|
||||||
|
@d.permission("can_use")
|
||||||
async refreshAllReactionRolesCmd(msg: Message) {
|
async refreshAllReactionRolesCmd(msg: Message) {
|
||||||
if (!this.isOwner(msg.author.id)) return;
|
|
||||||
|
|
||||||
const guilds = this.knub.getLoadedGuilds();
|
const guilds = this.knub.getLoadedGuilds();
|
||||||
for (const guild of guilds) {
|
for (const guild of guilds) {
|
||||||
if (guild.loadedPlugins.has("reaction_roles")) {
|
if (guild.loadedPlugins.has("reaction_roles")) {
|
||||||
|
@ -103,9 +130,8 @@ export class BotControlPlugin extends GlobalPlugin<IBotControlPluginConfig> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@d.command("guilds")
|
@d.command("guilds")
|
||||||
|
@d.permission("can_use")
|
||||||
async serversCmd(msg: Message) {
|
async serversCmd(msg: Message) {
|
||||||
if (!this.isOwner(msg.author.id)) return;
|
|
||||||
|
|
||||||
const joinedGuilds = Array.from(this.bot.guilds.values());
|
const joinedGuilds = Array.from(this.bot.guilds.values());
|
||||||
const loadedGuilds = this.knub.getLoadedGuilds();
|
const loadedGuilds = this.knub.getLoadedGuilds();
|
||||||
const loadedGuildsMap = loadedGuilds.reduce((map, guildData) => map.set(guildData.id, guildData), new Map());
|
const loadedGuildsMap = loadedGuilds.reduce((map, guildData) => map.set(guildData.id, guildData), new Map());
|
||||||
|
@ -122,9 +148,8 @@ export class BotControlPlugin extends GlobalPlugin<IBotControlPluginConfig> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@d.command("leave_guild", "<guildId:string>")
|
@d.command("leave_guild", "<guildId:string>")
|
||||||
|
@d.permission("can_use")
|
||||||
async leaveGuildCmd(msg: Message, args: { guildId: string }) {
|
async leaveGuildCmd(msg: Message, args: { guildId: string }) {
|
||||||
if (!this.isOwner(msg.author.id)) return;
|
|
||||||
|
|
||||||
if (!this.bot.guilds.has(args.guildId)) {
|
if (!this.bot.guilds.has(args.guildId)) {
|
||||||
msg.channel.createMessage(errorMessage("I am not in that guild"));
|
msg.channel.createMessage(errorMessage("I am not in that guild"));
|
||||||
return;
|
return;
|
||||||
|
@ -144,9 +169,8 @@ export class BotControlPlugin extends GlobalPlugin<IBotControlPluginConfig> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@d.command("reload_guild", "<guildId:string>")
|
@d.command("reload_guild", "<guildId:string>")
|
||||||
|
@d.permission("can_use")
|
||||||
async reloadGuildCmd(msg: Message, args: { guildId: string }) {
|
async reloadGuildCmd(msg: Message, args: { guildId: string }) {
|
||||||
if (!this.isOwner(msg.author.id)) return;
|
|
||||||
|
|
||||||
if (!this.bot.guilds.has(args.guildId)) {
|
if (!this.bot.guilds.has(args.guildId)) {
|
||||||
msg.channel.createMessage(errorMessage("I am not in that guild"));
|
msg.channel.createMessage(errorMessage("I am not in that guild"));
|
||||||
return;
|
return;
|
||||||
|
@ -164,9 +188,8 @@ export class BotControlPlugin extends GlobalPlugin<IBotControlPluginConfig> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@d.command("reload_all_guilds")
|
@d.command("reload_all_guilds")
|
||||||
|
@d.permission("can_use")
|
||||||
async reloadAllGuilds(msg: Message) {
|
async reloadAllGuilds(msg: Message) {
|
||||||
if (!this.isOwner(msg.author.id)) return;
|
|
||||||
|
|
||||||
const failedReloads: Map<string, string> = new Map();
|
const failedReloads: Map<string, string> = new Map();
|
||||||
let reloadCount = 0;
|
let reloadCount = 0;
|
||||||
|
|
||||||
|
@ -191,4 +214,29 @@ export class BotControlPlugin extends GlobalPlugin<IBotControlPluginConfig> {
|
||||||
msg.channel.createMessage(successMessage(`Reloaded ${reloadCount} guild(s)`));
|
msg.channel.createMessage(successMessage(`Reloaded ${reloadCount} guild(s)`));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@d.command("show_plugin_config", "<guildId:string> <pluginName:string>")
|
||||||
|
@d.permission("can_use")
|
||||||
|
async showPluginConfig(msg: Message, args: { guildId: string; pluginName: string }) {
|
||||||
|
const guildData = this.knub.getGuildData(args.guildId);
|
||||||
|
if (!guildData) {
|
||||||
|
msg.channel.createMessage(errorMessage(`Guild not loaded`));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const pluginInstance = guildData.loadedPlugins.get(args.pluginName);
|
||||||
|
if (!pluginInstance) {
|
||||||
|
msg.channel.createMessage(errorMessage(`Plugin not loaded`));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(pluginInstance instanceof ZeppelinPlugin)) {
|
||||||
|
msg.channel.createMessage(errorMessage(`Plugin is not a Zeppelin plugin`));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const opts = pluginInstance.getRuntimeOptions();
|
||||||
|
const archiveId = await this.archives.create(JSON.stringify(opts, null, 2), moment().add(15, "minutes"));
|
||||||
|
msg.channel.createMessage(this.archives.getUrl(this.knub.getGlobalConfig().url, archiveId));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue