Add bot owner commands for adding/removing servers and dashboard users
This commit is contained in:
parent
cd4b7a2f97
commit
5d13322439
8 changed files with 195 additions and 1 deletions
|
@ -39,4 +39,18 @@ export class AllowedGuilds extends BaseRepository {
|
||||||
updateInfo(id, name, icon, ownerId) {
|
updateInfo(id, name, icon, ownerId) {
|
||||||
return this.allowedGuilds.update({ id }, { name, icon, owner_id: ownerId });
|
return this.allowedGuilds.update({ id }, { name, icon, owner_id: ownerId });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
add(id, data: Partial<Omit<AllowedGuild, "id">> = {}) {
|
||||||
|
return this.allowedGuilds.insert({
|
||||||
|
name: "Server",
|
||||||
|
icon: null,
|
||||||
|
owner_id: "0",
|
||||||
|
...data,
|
||||||
|
id,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
remove(id) {
|
||||||
|
return this.allowedGuilds.delete({ id });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import { getRepository, Repository } from "typeorm";
|
import { getRepository, Repository } from "typeorm";
|
||||||
import { ApiPermissionAssignment } from "./entities/ApiPermissionAssignment";
|
import { ApiPermissionAssignment } from "./entities/ApiPermissionAssignment";
|
||||||
import { BaseRepository } from "./BaseRepository";
|
import { BaseRepository } from "./BaseRepository";
|
||||||
|
import { ApiPermissions } from "@shared/apiPermissions";
|
||||||
|
|
||||||
export enum ApiPermissionTypes {
|
export enum ApiPermissionTypes {
|
||||||
User = "USER",
|
User = "USER",
|
||||||
|
@ -41,4 +42,17 @@ export class ApiPermissionAssignments extends BaseRepository {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
addUser(guildId, userId, permissions: ApiPermissions[]) {
|
||||||
|
return this.apiPermissions.insert({
|
||||||
|
guild_id: guildId,
|
||||||
|
type: ApiPermissionTypes.User,
|
||||||
|
target_id: userId,
|
||||||
|
permissions,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
removeUser(guildId, userId) {
|
||||||
|
return this.apiPermissions.delete({ guild_id: guildId, type: ApiPermissionTypes.User, target_id: userId });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,11 @@ import { ReloadGlobalPluginsCmd } from "./commands/ReloadGlobalPluginsCmd";
|
||||||
import { ServersCmd } from "./commands/ServersCmd";
|
import { ServersCmd } from "./commands/ServersCmd";
|
||||||
import { LeaveServerCmd } from "./commands/LeaveServerCmd";
|
import { LeaveServerCmd } from "./commands/LeaveServerCmd";
|
||||||
import { ReloadServerCmd } from "./commands/ReloadServerCmd";
|
import { ReloadServerCmd } from "./commands/ReloadServerCmd";
|
||||||
|
import { AllowedGuilds } from "../../data/AllowedGuilds";
|
||||||
|
import { AllowServerCmd } from "./commands/AllowServerCmd";
|
||||||
|
import { DisallowServerCmd } from "./commands/DisallowServerCmd";
|
||||||
|
import { AddDashboardUserCmd } from "./commands/AddDashboardUserCmd";
|
||||||
|
import { RemoveDashboardUserCmd } from "./commands/RemoveDashboardUserCmd";
|
||||||
|
|
||||||
const defaultOptions = {
|
const defaultOptions = {
|
||||||
config: {
|
config: {
|
||||||
|
@ -20,10 +25,21 @@ export const BotControlPlugin = zeppelinGlobalPlugin<BotControlPluginType>()("bo
|
||||||
configSchema: ConfigSchema,
|
configSchema: ConfigSchema,
|
||||||
defaultOptions,
|
defaultOptions,
|
||||||
|
|
||||||
commands: [ReloadGlobalPluginsCmd, ServersCmd, LeaveServerCmd, ReloadServerCmd],
|
// prettier-ignore
|
||||||
|
commands: [
|
||||||
|
ReloadGlobalPluginsCmd,
|
||||||
|
ServersCmd,
|
||||||
|
LeaveServerCmd,
|
||||||
|
ReloadServerCmd,
|
||||||
|
AllowServerCmd,
|
||||||
|
DisallowServerCmd,
|
||||||
|
AddDashboardUserCmd,
|
||||||
|
RemoveDashboardUserCmd,
|
||||||
|
],
|
||||||
|
|
||||||
onLoad(pluginData) {
|
onLoad(pluginData) {
|
||||||
pluginData.state.archives = new GuildArchives(0);
|
pluginData.state.archives = new GuildArchives(0);
|
||||||
|
pluginData.state.allowedGuilds = new AllowedGuilds();
|
||||||
|
|
||||||
if (getActiveReload()) {
|
if (getActiveReload()) {
|
||||||
const [guildId, channelId] = getActiveReload();
|
const [guildId, channelId] = getActiveReload();
|
||||||
|
|
|
@ -0,0 +1,46 @@
|
||||||
|
import { botControlCmd } from "../types";
|
||||||
|
import { isOwnerPreFilter, sendErrorMessage, sendSuccessMessage } from "../../../pluginUtils";
|
||||||
|
import { commandTypeHelpers as ct } from "../../../commandTypes";
|
||||||
|
import { ApiPermissions } from "@shared/apiPermissions";
|
||||||
|
|
||||||
|
export const AddDashboardUserCmd = botControlCmd({
|
||||||
|
trigger: ["add_dashboard_user"],
|
||||||
|
permission: null,
|
||||||
|
config: {
|
||||||
|
preFilters: [isOwnerPreFilter],
|
||||||
|
},
|
||||||
|
|
||||||
|
signature: {
|
||||||
|
guildId: ct.string(),
|
||||||
|
users: ct.user({ rest: true }),
|
||||||
|
},
|
||||||
|
|
||||||
|
async run({ pluginData, message: msg, args }) {
|
||||||
|
const guild = await pluginData.state.allowedGuilds.find(args.guildId);
|
||||||
|
if (!guild) {
|
||||||
|
sendErrorMessage(pluginData, msg.channel, "Server is not using Zeppelin");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const user of args.users) {
|
||||||
|
const existingAssignment = await pluginData.state.apiPermissionAssignments.getByGuildAndUserId(
|
||||||
|
args.guildId,
|
||||||
|
user.id,
|
||||||
|
);
|
||||||
|
if (existingAssignment) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
await pluginData.state.apiPermissionAssignments.addUser(args.guildId, user.id, [ApiPermissions.EditConfig]);
|
||||||
|
}
|
||||||
|
|
||||||
|
const userNameList = args.users.map(
|
||||||
|
user => `<@!${user.id}> (**${user.username}#${user.discriminator}**, \`${user.id}\`)`,
|
||||||
|
);
|
||||||
|
sendSuccessMessage(
|
||||||
|
pluginData,
|
||||||
|
msg.channel,
|
||||||
|
`The following users were given dashboard access for **${guild.name}**:\n\n${userNameList}`,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
});
|
26
backend/src/plugins/BotControl/commands/AllowServerCmd.ts
Normal file
26
backend/src/plugins/BotControl/commands/AllowServerCmd.ts
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
import { botControlCmd } from "../types";
|
||||||
|
import { isOwnerPreFilter, sendErrorMessage, sendSuccessMessage } from "../../../pluginUtils";
|
||||||
|
import { commandTypeHelpers as ct } from "../../../commandTypes";
|
||||||
|
|
||||||
|
export const AllowServerCmd = botControlCmd({
|
||||||
|
trigger: ["allow_server", "allowserver", "add_server", "addserver"],
|
||||||
|
permission: null,
|
||||||
|
config: {
|
||||||
|
preFilters: [isOwnerPreFilter],
|
||||||
|
},
|
||||||
|
|
||||||
|
signature: {
|
||||||
|
guildId: ct.string(),
|
||||||
|
},
|
||||||
|
|
||||||
|
async run({ pluginData, message: msg, args }) {
|
||||||
|
const existing = await pluginData.state.allowedGuilds.find(args.guildId);
|
||||||
|
if (existing) {
|
||||||
|
sendErrorMessage(pluginData, msg.channel, "Server is already allowed!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await pluginData.state.allowedGuilds.add(args.guildId);
|
||||||
|
sendSuccessMessage(pluginData, msg.channel, "Server is now allowed to use Zeppelin!");
|
||||||
|
},
|
||||||
|
});
|
28
backend/src/plugins/BotControl/commands/DisallowServerCmd.ts
Normal file
28
backend/src/plugins/BotControl/commands/DisallowServerCmd.ts
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
import { botControlCmd } from "../types";
|
||||||
|
import { isOwnerPreFilter, sendErrorMessage, sendSuccessMessage } from "../../../pluginUtils";
|
||||||
|
import { commandTypeHelpers as ct } from "../../../commandTypes";
|
||||||
|
import { noop } from "../../../utils";
|
||||||
|
|
||||||
|
export const DisallowServerCmd = botControlCmd({
|
||||||
|
trigger: ["disallow_server", "disallowserver", "remove_server", "removeserver"],
|
||||||
|
permission: null,
|
||||||
|
config: {
|
||||||
|
preFilters: [isOwnerPreFilter],
|
||||||
|
},
|
||||||
|
|
||||||
|
signature: {
|
||||||
|
guildId: ct.string(),
|
||||||
|
},
|
||||||
|
|
||||||
|
async run({ pluginData, message: msg, args }) {
|
||||||
|
const existing = await pluginData.state.allowedGuilds.find(args.guildId);
|
||||||
|
if (!existing) {
|
||||||
|
sendErrorMessage(pluginData, msg.channel, "That server is not allowed in the first place!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await pluginData.state.allowedGuilds.remove(args.guildId);
|
||||||
|
await pluginData.client.leaveGuild(args.guildId).catch(noop);
|
||||||
|
sendSuccessMessage(pluginData, msg.channel, "Server removed!");
|
||||||
|
},
|
||||||
|
});
|
|
@ -0,0 +1,46 @@
|
||||||
|
import { botControlCmd } from "../types";
|
||||||
|
import { isOwnerPreFilter, sendErrorMessage, sendSuccessMessage } from "../../../pluginUtils";
|
||||||
|
import { commandTypeHelpers as ct } from "../../../commandTypes";
|
||||||
|
import { ApiPermissions } from "@shared/apiPermissions";
|
||||||
|
|
||||||
|
export const RemoveDashboardUserCmd = botControlCmd({
|
||||||
|
trigger: ["remove_dashboard_user"],
|
||||||
|
permission: null,
|
||||||
|
config: {
|
||||||
|
preFilters: [isOwnerPreFilter],
|
||||||
|
},
|
||||||
|
|
||||||
|
signature: {
|
||||||
|
guildId: ct.string(),
|
||||||
|
users: ct.user({ rest: true }),
|
||||||
|
},
|
||||||
|
|
||||||
|
async run({ pluginData, message: msg, args }) {
|
||||||
|
const guild = await pluginData.state.allowedGuilds.find(args.guildId);
|
||||||
|
if (!guild) {
|
||||||
|
sendErrorMessage(pluginData, msg.channel, "Server is not using Zeppelin");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const user of args.users) {
|
||||||
|
const existingAssignment = await pluginData.state.apiPermissionAssignments.getByGuildAndUserId(
|
||||||
|
args.guildId,
|
||||||
|
user.id,
|
||||||
|
);
|
||||||
|
if (!existingAssignment) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
await pluginData.state.apiPermissionAssignments.removeUser(args.guildId, user.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
const userNameList = args.users.map(
|
||||||
|
user => `<@!${user.id}> (**${user.username}#${user.discriminator}**, \`${user.id}\`)`,
|
||||||
|
);
|
||||||
|
sendSuccessMessage(
|
||||||
|
pluginData,
|
||||||
|
msg.channel,
|
||||||
|
`The following users were removed from the dashboard for **${guild.name}**:\n\n${userNameList}`,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
});
|
|
@ -2,6 +2,8 @@ import * as t from "io-ts";
|
||||||
import { tNullable } from "../../utils";
|
import { tNullable } from "../../utils";
|
||||||
import { BasePluginType, globalCommand, globalEventListener } from "knub";
|
import { BasePluginType, globalCommand, globalEventListener } from "knub";
|
||||||
import { GuildArchives } from "../../data/GuildArchives";
|
import { GuildArchives } from "../../data/GuildArchives";
|
||||||
|
import { AllowedGuilds } from "../../data/AllowedGuilds";
|
||||||
|
import { ApiPermissionAssignments } from "../../data/ApiPermissionAssignments";
|
||||||
|
|
||||||
export const ConfigSchema = t.type({
|
export const ConfigSchema = t.type({
|
||||||
can_use: t.boolean,
|
can_use: t.boolean,
|
||||||
|
@ -13,6 +15,8 @@ export interface BotControlPluginType extends BasePluginType {
|
||||||
config: TConfigSchema;
|
config: TConfigSchema;
|
||||||
state: {
|
state: {
|
||||||
archives: GuildArchives;
|
archives: GuildArchives;
|
||||||
|
allowedGuilds: AllowedGuilds;
|
||||||
|
apiPermissionAssignments: ApiPermissionAssignments;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue