mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-03-15 05:41:51 +00:00
CompanionChannels: refactor config format, don't do unnecessary permission changes
This commit is contained in:
parent
0ea655ecee
commit
20d7d77178
1 changed files with 64 additions and 38 deletions
|
@ -1,68 +1,95 @@
|
|||
import { decorators as d, IPluginOptions, logger } from "knub";
|
||||
import { ZeppelinPlugin } from "./ZeppelinPlugin";
|
||||
import { Member, Channel, GuildChannel, PermissionOverwrite, Permission, Message } from "eris";
|
||||
import { Member, Channel, GuildChannel, PermissionOverwrite, Permission, Message, TextChannel } from "eris";
|
||||
import * as t from "io-ts";
|
||||
import { tNullable } from "../utils";
|
||||
|
||||
// Permissions using these numbers: https://abal.moe/Eris/docs/reference (add all allowed/denied ones up)
|
||||
const CompanionChannel = t.type({
|
||||
channelIds: t.array(t.string),
|
||||
const CompanionChannelOpts = t.type({
|
||||
voiceChannelIds: t.array(t.string),
|
||||
textChannelIds: t.array(t.string),
|
||||
permissions: t.number,
|
||||
enabled: tNullable(t.boolean),
|
||||
});
|
||||
type TCompanionChannel = t.TypeOf<typeof CompanionChannel>;
|
||||
type TCompanionChannelOpts = t.TypeOf<typeof CompanionChannelOpts>;
|
||||
|
||||
const ConfigSchema = t.type({
|
||||
channels: t.record(t.string, CompanionChannel),
|
||||
companions: t.record(t.string, CompanionChannelOpts),
|
||||
});
|
||||
type TConfigSchema = t.TypeOf<typeof ConfigSchema>;
|
||||
|
||||
interface ICompanionChannelMap {
|
||||
[channelId: string]: TCompanionChannel;
|
||||
[channelId: string]: TCompanionChannelOpts;
|
||||
}
|
||||
|
||||
const defaultCompanionChannelOpts: Partial<TCompanionChannelOpts> = {
|
||||
enabled: true,
|
||||
};
|
||||
|
||||
export class CompanionChannelPlugin extends ZeppelinPlugin<TConfigSchema> {
|
||||
public static pluginName = "companion_channels";
|
||||
protected static configSchema = ConfigSchema;
|
||||
|
||||
companionChannels: Map<string, TCompanionChannel> = new Map();
|
||||
|
||||
protected static getStaticDefaultOptions(): IPluginOptions<TConfigSchema> {
|
||||
return {
|
||||
config: {
|
||||
channels: {},
|
||||
companions: {},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
onLoad() {
|
||||
const tempCompanionChannels: ICompanionChannelMap = this.getConfig().channels;
|
||||
/**
|
||||
* Returns an array of companion channel opts that match the given userId and voiceChannelId,
|
||||
* with default companion channel opts applied as well
|
||||
*/
|
||||
protected getCompanionChannelOptsForVoiceChannelId(userId, voiceChannelId): TCompanionChannelOpts[] {
|
||||
const config = this.getConfigForMemberIdAndChannelId(userId, voiceChannelId);
|
||||
return Object.values(config.companions)
|
||||
.filter(opts => opts.voiceChannelIds.includes(voiceChannelId))
|
||||
.map(opts => Object.assign({}, defaultCompanionChannelOpts, opts));
|
||||
}
|
||||
|
||||
for (const [channelId, opts] of Object.entries(tempCompanionChannels)) {
|
||||
this.companionChannels.set(channelId, opts);
|
||||
async handleCompanionPermissions(userId: string, voiceChannelId?: string, oldChannelId?: string) {
|
||||
const permsToDelete: Set<string> = new Set(); // channelId[]
|
||||
const oldPerms: Map<string, number> = new Map(); // channelId => permissions
|
||||
const permsToSet: Map<string, number> = new Map(); // channelId => permissions
|
||||
|
||||
const oldChannelOptsArr: TCompanionChannelOpts[] = oldChannelId
|
||||
? this.getCompanionChannelOptsForVoiceChannelId(userId, oldChannelId)
|
||||
: [];
|
||||
const newChannelOptsArr: TCompanionChannelOpts[] = voiceChannelId
|
||||
? this.getCompanionChannelOptsForVoiceChannelId(userId, voiceChannelId)
|
||||
: [];
|
||||
|
||||
for (const oldChannelOpts of oldChannelOptsArr) {
|
||||
for (const channelId of oldChannelOpts.textChannelIds) {
|
||||
oldPerms.set(channelId, oldChannelOpts.permissions);
|
||||
permsToDelete.add(channelId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
onUnload() {
|
||||
this.companionChannels.clear();
|
||||
}
|
||||
|
||||
async handleCompanionPermissions(userID: string, voiceChannelId: string, remove?: boolean) {
|
||||
if (this.companionChannels.has(voiceChannelId)) {
|
||||
const compChannels = this.companionChannels.get(voiceChannelId);
|
||||
compChannels.channelIds.forEach(textChannelId => {
|
||||
const textChannel = <GuildChannel>this.bot.getChannel(textChannelId);
|
||||
|
||||
if (remove) {
|
||||
textChannel.deletePermission(userID, `Companion Channel for ${voiceChannelId} | User Left`);
|
||||
} else {
|
||||
textChannel.editPermission(
|
||||
userID,
|
||||
compChannels.permissions,
|
||||
0,
|
||||
"member",
|
||||
`Companion Channel for ${voiceChannelId} | User Joined`,
|
||||
);
|
||||
for (const newChannelOpts of newChannelOptsArr) {
|
||||
for (const channelId of newChannelOpts.textChannelIds) {
|
||||
if (oldPerms.get(channelId) !== newChannelOpts.permissions) {
|
||||
// Update text channel perms if the channel we transitioned from didn't already have the same text channel perms
|
||||
permsToSet.set(channelId, newChannelOpts.permissions);
|
||||
}
|
||||
});
|
||||
if (permsToDelete.has(channelId)) {
|
||||
permsToDelete.delete(channelId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const channelId of permsToDelete) {
|
||||
const channel = this.guild.channels.get(channelId);
|
||||
if (!channel || !(channel instanceof TextChannel)) continue;
|
||||
channel.deletePermission(userId, `Companion Channel for ${oldChannelId} | User Left`);
|
||||
}
|
||||
|
||||
for (const [channelId, permissions] of permsToSet) {
|
||||
const channel = this.guild.channels.get(channelId);
|
||||
if (!channel || !(channel instanceof TextChannel)) continue;
|
||||
channel.editPermission(userId, permissions, 0, "member", `Companion Channel for ${voiceChannelId} | User Joined`);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -73,12 +100,11 @@ export class CompanionChannelPlugin extends ZeppelinPlugin<TConfigSchema> {
|
|||
|
||||
@d.event("voiceChannelSwitch")
|
||||
onVoiceChannelSwitch(member: Member, newChannel: Channel, oldChannel: Channel) {
|
||||
this.handleCompanionPermissions(member.id, oldChannel.id, true);
|
||||
this.handleCompanionPermissions(member.id, newChannel.id);
|
||||
this.handleCompanionPermissions(member.id, newChannel.id, oldChannel.id);
|
||||
}
|
||||
|
||||
@d.event("voiceChannelLeave")
|
||||
onVoiceChannelLeave(member: Member, voiceChannel: Channel) {
|
||||
this.handleCompanionPermissions(member.id, voiceChannel.id, true);
|
||||
this.handleCompanionPermissions(member.id, null, voiceChannel.id);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue