2019-05-26 04:00:59 +02:00
|
|
|
import { decorators as d, IPluginOptions, logger } from "knub";
|
|
|
|
import { ZeppelinPlugin } from "./ZeppelinPlugin";
|
|
|
|
import { Member, Channel, GuildChannel, PermissionOverwrite, Permission, Message } from "eris";
|
2019-07-21 21:15:52 +03:00
|
|
|
import * as t from "io-ts";
|
2019-05-26 04:00:59 +02:00
|
|
|
|
|
|
|
// Permissions using these numbers: https://abal.moe/Eris/docs/reference (add all allowed/denied ones up)
|
2019-07-21 21:15:52 +03:00
|
|
|
const CompanionChannel = t.type({
|
|
|
|
channelIds: t.array(t.string),
|
|
|
|
permissions: t.number,
|
|
|
|
});
|
|
|
|
type TCompanionChannel = t.TypeOf<typeof CompanionChannel>;
|
2019-05-26 04:00:59 +02:00
|
|
|
|
2019-07-21 21:15:52 +03:00
|
|
|
const ConfigSchema = t.type({
|
|
|
|
channels: t.record(t.string, CompanionChannel),
|
|
|
|
});
|
|
|
|
type TConfigSchema = t.TypeOf<typeof ConfigSchema>;
|
2019-05-26 04:00:59 +02:00
|
|
|
|
2019-07-21 21:15:52 +03:00
|
|
|
interface ICompanionChannelMap {
|
|
|
|
[channelId: string]: TCompanionChannel;
|
2019-05-26 04:00:59 +02:00
|
|
|
}
|
|
|
|
|
2019-07-21 21:15:52 +03:00
|
|
|
export class CompanionChannelPlugin extends ZeppelinPlugin<TConfigSchema> {
|
2019-05-26 04:00:59 +02:00
|
|
|
public static pluginName = "companion_channels";
|
2019-07-21 21:15:52 +03:00
|
|
|
protected static configSchema = ConfigSchema;
|
2019-05-26 04:00:59 +02:00
|
|
|
|
2019-07-21 21:15:52 +03:00
|
|
|
companionChannels: Map<string, TCompanionChannel> = new Map();
|
2019-05-26 04:00:59 +02:00
|
|
|
|
2019-07-22 00:09:45 +03:00
|
|
|
protected static getStaticDefaultOptions(): IPluginOptions<TConfigSchema> {
|
2019-05-26 04:00:59 +02:00
|
|
|
return {
|
|
|
|
config: {
|
|
|
|
channels: {},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
onLoad() {
|
|
|
|
const tempCompanionChannels: ICompanionChannelMap = this.getConfig().channels;
|
|
|
|
|
|
|
|
for (const [channelId, opts] of Object.entries(tempCompanionChannels)) {
|
|
|
|
this.companionChannels.set(channelId, opts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onUnload() {
|
2019-07-11 12:29:50 +03:00
|
|
|
this.companionChannels.clear();
|
2019-05-26 04:00:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@d.event("voiceChannelJoin")
|
|
|
|
onVoiceChannelJoin(member: Member, voiceChannel: Channel) {
|
|
|
|
this.handleCompanionPermissions(member.id, voiceChannel.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
@d.event("voiceChannelSwitch")
|
|
|
|
onVoiceChannelSwitch(member: Member, newChannel: Channel, oldChannel: Channel) {
|
|
|
|
this.handleCompanionPermissions(member.id, oldChannel.id, true);
|
|
|
|
this.handleCompanionPermissions(member.id, newChannel.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
@d.event("voiceChannelLeave")
|
|
|
|
onVoiceChannelLeave(member: Member, voiceChannel: Channel) {
|
|
|
|
this.handleCompanionPermissions(member.id, voiceChannel.id, true);
|
|
|
|
}
|
|
|
|
}
|