2020-01-09 00:23:41 +11:00
|
|
|
import { ZeppelinPlugin, trimPluginDescription } from "./ZeppelinPlugin";
|
|
|
|
import * as t from "io-ts";
|
2020-01-09 01:32:12 +11:00
|
|
|
import { tNullable } from "../utils";
|
2020-01-09 00:23:41 +11:00
|
|
|
import { decorators as d, IPluginOptions, logger, waitForReaction, waitForReply } from "knub";
|
|
|
|
import { Attachment, Constants as ErisConstants, Guild, Member, Message, TextChannel, User } from "eris";
|
2020-01-09 01:32:12 +11:00
|
|
|
import { GuildLogs } from "../data/GuildLogs";
|
2020-01-09 00:23:41 +11:00
|
|
|
|
|
|
|
const ConfigSchema = t.type({
|
|
|
|
can_assign: t.boolean,
|
|
|
|
assignable_roles: tNullable(t.array(t.string))
|
|
|
|
});
|
|
|
|
type TConfigSchema = t.TypeOf<typeof ConfigSchema>;
|
|
|
|
|
|
|
|
enum RoleActions{
|
2020-01-09 01:32:12 +11:00
|
|
|
Add = 1,
|
2020-01-09 00:23:41 +11:00
|
|
|
Remove
|
|
|
|
};
|
|
|
|
|
|
|
|
export class RolesPlugin extends ZeppelinPlugin<TConfigSchema> {
|
|
|
|
public static pluginName = "roles";
|
|
|
|
public static configSchema = ConfigSchema;
|
|
|
|
|
|
|
|
public static pluginInfo = {
|
|
|
|
prettyName: "Roles",
|
|
|
|
description: trimPluginDescription(`
|
|
|
|
Enables authorised users to add and remove whitelisted roles with a command.
|
|
|
|
`),
|
|
|
|
};
|
|
|
|
protected logs: GuildLogs;
|
|
|
|
|
|
|
|
onLoad(){
|
|
|
|
this.logs = new GuildLogs(this.guildId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static getStaticDefaultOptions(): IPluginOptions<TConfigSchema> {
|
|
|
|
return {
|
|
|
|
config: {
|
|
|
|
can_assign: false,
|
|
|
|
assignable_roles: null
|
|
|
|
},
|
|
|
|
overrides: [
|
|
|
|
{
|
|
|
|
level: ">=50",
|
|
|
|
config: {
|
|
|
|
can_assign: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-10 01:04:58 +11:00
|
|
|
@d.command("role", "<action:string> <user:string> [role:string$]",{
|
2020-01-09 00:23:41 +11:00
|
|
|
extra: {
|
|
|
|
info: {
|
|
|
|
description: "Assign a permitted role to a user",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
@d.permission("can_assign")
|
2020-01-09 01:32:12 +11:00
|
|
|
async assignRole(msg: Message, args: {action: string; user: string; role: string}){
|
2020-01-09 00:23:41 +11:00
|
|
|
const user = await this.resolveUser(args.user);
|
2020-01-10 01:04:58 +11:00
|
|
|
const roleId = await this.resolveRoleId(args.role);
|
2020-01-09 01:32:12 +11:00
|
|
|
if (user.discriminator == "0000") {
|
2020-01-09 00:23:41 +11:00
|
|
|
return this.sendErrorMessage(msg.channel, `User not found`);
|
|
|
|
}
|
|
|
|
|
|
|
|
//if the role doesnt exist, we can exit
|
|
|
|
let roleIds = (msg.channel as TextChannel).guild.roles.map(x => x.id)
|
2020-01-10 01:04:58 +11:00
|
|
|
if(!(roleIds.includes(roleId))){
|
2020-01-09 00:23:41 +11:00
|
|
|
return this.sendErrorMessage(msg.channel, `Role not found`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the user exists as a guild member, make sure we can act on them first
|
2020-01-10 01:04:58 +11:00
|
|
|
const targetMember = await this.getMember(user.id);
|
|
|
|
if (targetMember && !this.canActOn(msg.member, targetMember)) {
|
2020-01-09 00:23:41 +11:00
|
|
|
this.sendErrorMessage(msg.channel, "Cannot add or remove roles on this user: insufficient permissions");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const action: string = args.action[0].toUpperCase() + args.action.slice(1).toLowerCase();
|
|
|
|
if(!RoleActions[action]){
|
|
|
|
this.sendErrorMessage(msg.channel, "Cannot add or remove roles on this user: invalid action");
|
2020-01-09 01:32:12 +11:00
|
|
|
return;
|
2020-01-09 00:23:41 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
//check if the role is allowed to be applied
|
2020-01-09 01:32:12 +11:00
|
|
|
let config = this.getConfigForMsg(msg)
|
2020-01-10 01:04:58 +11:00
|
|
|
if(!config.assignable_roles || !config.assignable_roles.includes(roleId)){
|
2020-01-09 01:32:12 +11:00
|
|
|
this.sendErrorMessage(msg.channel, "You do not have access to the specified role");
|
|
|
|
return;
|
|
|
|
}
|
2020-01-10 01:04:58 +11:00
|
|
|
//at this point, everything has been verified, so it's ACTION TIME
|
|
|
|
switch(RoleActions[action]){
|
|
|
|
case RoleActions.Add:
|
|
|
|
if(targetMember.roles.includes(roleId)){
|
|
|
|
this.sendErrorMessage(msg.channel, "Role already applied to user");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
await this.bot.addGuildMemberRole(this.guildId, user.id, roleId);
|
|
|
|
this.sendSuccessMessage(msg.channel, `Role added to user!`);
|
|
|
|
break;
|
|
|
|
case RoleActions.Remove:
|
|
|
|
if(!targetMember.roles.includes(roleId)){
|
|
|
|
this.sendErrorMessage(msg.channel, "User does not have role");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
await this.bot.removeGuildMemberRole(this.guildId, user.id, roleId);
|
|
|
|
this.sendSuccessMessage(msg.channel, `Role removed from user!`);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-01-09 00:23:41 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|