3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-13 13:25:03 +00:00

Port BotControl

This commit is contained in:
Dragory 2020-07-30 03:21:07 +03:00
parent b66e601cef
commit f500034729
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
10 changed files with 237 additions and 2 deletions

View file

@ -0,0 +1,35 @@
import { command } from "knub";
import { BotControlPluginType } from "../types";
import { isOwnerPreFilter, sendErrorMessage, sendSuccessMessage } from "../../../pluginUtils";
import { commandTypeHelpers as ct } from "../../../commandTypes";
export const LeaveServerCmd = command<BotControlPluginType>()({
trigger: ["leave_server", "leave_guild"],
permission: null,
config: {
preFilters: [isOwnerPreFilter],
},
signature: {
guildId: ct.string(),
},
async run({ pluginData, message: msg, args }) {
if (!pluginData.client.guilds.has(args.guildId)) {
sendErrorMessage(pluginData, msg.channel, "I am not in that guild");
return;
}
const guildToLeave = pluginData.client.guilds.get(args.guildId);
const guildName = guildToLeave.name;
try {
await pluginData.client.leaveGuild(args.guildId);
} catch (e) {
sendErrorMessage(pluginData, msg.channel, `Failed to leave guild: ${e.message}`);
return;
}
sendSuccessMessage(pluginData, msg.channel, `Left guild **${guildName}**`);
},
});

View file

@ -0,0 +1,22 @@
import { command } from "knub";
import { BotControlPluginType } from "../types";
import { isOwnerPreFilter } from "../../../pluginUtils";
import { getActiveReload, setActiveReload } from "../activeReload";
import { TextChannel } from "eris";
export const ReloadGlobalPluginsCmd = command<BotControlPluginType>()({
trigger: "bot_reload_global_plugins",
permission: null,
config: {
preFilters: [isOwnerPreFilter],
},
async run({ pluginData, message }) {
if (getActiveReload()) return;
setActiveReload((message.channel as TextChannel).guild?.id, message.channel.id);
await message.channel.createMessage("Reloading global plugins...");
pluginData.getKnubInstance().reloadAllGlobalPlugins();
},
});

View file

@ -0,0 +1,33 @@
import { command } from "knub";
import { BotControlPluginType } from "../types";
import { isOwnerPreFilter, sendErrorMessage, sendSuccessMessage } from "../../../pluginUtils";
import { commandTypeHelpers as ct } from "../../../commandTypes";
export const ReloadServerCmd = command<BotControlPluginType>()({
trigger: ["reload_server", "reload_guild"],
permission: null,
config: {
preFilters: [isOwnerPreFilter],
},
signature: {
guildId: ct.string(),
},
async run({ pluginData, message: msg, args }) {
if (!pluginData.client.guilds.has(args.guildId)) {
sendErrorMessage(pluginData, msg.channel, "I am not in that guild");
return;
}
try {
await pluginData.getKnubInstance().reloadGuild(args.guildId);
} catch (e) {
sendErrorMessage(pluginData, msg.channel, `Failed to reload guild: ${e.message}`);
return;
}
const guild = pluginData.client.guilds.get(args.guildId);
sendSuccessMessage(pluginData, msg.channel, `Reloaded guild **${guild?.name || "???"}**`);
},
});

View file

@ -0,0 +1,69 @@
import { command } from "knub";
import { BotControlPluginType } from "../types";
import { isOwnerPreFilter } from "../../../pluginUtils";
import { commandTypeHelpers as ct } from "../../../commandTypes";
import escapeStringRegexp from "escape-string-regexp";
import { createChunkedMessage, sorter } from "../../../utils";
export const ServersCmd = command<BotControlPluginType>()({
trigger: ["servers", "guilds"],
permission: null,
config: {
preFilters: [isOwnerPreFilter],
},
signature: {
search: ct.string({ catchAll: true, required: false }),
all: ct.switchOption({ shortcut: "a" }),
initialized: ct.switchOption({ shortcut: "i" }),
uninitialized: ct.switchOption({ shortcut: "u" }),
},
async run({ pluginData, message: msg, args }) {
const showList = Boolean(args.all || args.initialized || args.uninitialized || args.search);
const search = args.search && new RegExp([...args.search].map(s => escapeStringRegexp(s)).join(".*"), "i");
const joinedGuilds = Array.from(pluginData.client.guilds.values());
const loadedGuilds = pluginData.getKnubInstance().getLoadedGuilds();
const loadedGuildsMap = loadedGuilds.reduce((map, guildData) => map.set(guildData.guildId, guildData), new Map());
if (showList) {
let filteredGuilds = Array.from(joinedGuilds);
if (args.initialized) {
filteredGuilds = filteredGuilds.filter(g => loadedGuildsMap.has(g.id));
}
if (args.uninitialized) {
filteredGuilds = filteredGuilds.filter(g => !loadedGuildsMap.has(g.id));
}
if (args.search) {
filteredGuilds = filteredGuilds.filter(g => search.test(`${g.id} ${g.name}`));
}
if (filteredGuilds.length) {
filteredGuilds.sort(sorter(g => g.name.toLowerCase()));
const longestId = filteredGuilds.reduce((longest, guild) => Math.max(longest, guild.id.length), 0);
const lines = filteredGuilds.map(g => {
const paddedId = g.id.padEnd(longestId, " ");
return `\`${paddedId}\` **${g.name}** (${loadedGuildsMap.has(g.id) ? "initialized" : "not initialized"}) (${
g.memberCount
} members)`;
});
createChunkedMessage(msg.channel, lines.join("\n"));
} else {
msg.channel.createMessage("No servers matched the filters");
}
} else {
const total = joinedGuilds.length;
const initialized = joinedGuilds.filter(g => loadedGuildsMap.has(g.id)).length;
const unInitialized = total - initialized;
msg.channel.createMessage(
`I am on **${total} total servers**, of which **${initialized} are initialized** and **${unInitialized} are not initialized**`,
);
}
},
});