2020-08-10 02:22:39 +03:00
|
|
|
import * as t from "io-ts";
|
|
|
|
import { automodTrigger } from "../helpers";
|
|
|
|
import { consumeIgnoredRoleChange } from "../functions/ignoredRoleChanges";
|
|
|
|
|
|
|
|
interface RoleAddedMatchResult {
|
|
|
|
matchedRoleId: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const RoleAddedTrigger = automodTrigger<RoleAddedMatchResult>()({
|
|
|
|
configType: t.union([t.string, t.array(t.string)]),
|
|
|
|
|
2020-11-09 20:03:57 +02:00
|
|
|
defaultConfig: [],
|
2020-08-10 02:22:39 +03:00
|
|
|
|
|
|
|
async match({ triggerConfig, context, pluginData }) {
|
2020-11-09 20:03:57 +02:00
|
|
|
if (!context.member || !context.rolesChanged || context.rolesChanged.added!.length === 0) {
|
2020-08-10 02:22:39 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const triggerRoles = Array.isArray(triggerConfig) ? triggerConfig : [triggerConfig];
|
|
|
|
for (const roleId of triggerRoles) {
|
2020-11-09 20:03:57 +02:00
|
|
|
if (context.rolesChanged.added!.includes(roleId)) {
|
2020-08-10 02:22:39 +03:00
|
|
|
if (consumeIgnoredRoleChange(pluginData, context.member.id, roleId)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
extra: {
|
|
|
|
matchedRoleId: roleId,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
renderMatchInformation({ matchResult, pluginData, contexts }) {
|
|
|
|
const role = pluginData.guild.roles.get(matchResult.extra.matchedRoleId);
|
|
|
|
const roleName = role?.name || "Unknown";
|
|
|
|
const member = contexts[0].member!;
|
|
|
|
const memberName = `**${member.user.username}#${member.user.discriminator}** (\`${member.id}\`)`;
|
|
|
|
return `Role ${roleName} (\`${matchResult.extra.matchedRoleId}\`) was added to ${memberName}`;
|
|
|
|
},
|
|
|
|
});
|