Migrate ReactionRoles to new Plugin structure
This commit is contained in:
parent
140ba84544
commit
c0b7bea35d
12 changed files with 512 additions and 0 deletions
|
@ -0,0 +1,59 @@
|
|||
import { PluginData } from "knub";
|
||||
import { ReactionRolesPluginType, RoleChangeMode, PendingMemberRoleChanges } from "../types";
|
||||
import { resolveMember } from "src/utils";
|
||||
import { logger } from "src/logger";
|
||||
|
||||
const ROLE_CHANGE_BATCH_DEBOUNCE_TIME = 1500;
|
||||
|
||||
export async function addMemberPendingRoleChange(
|
||||
pluginData: PluginData<ReactionRolesPluginType>,
|
||||
memberId: string,
|
||||
mode: RoleChangeMode,
|
||||
roleId: string,
|
||||
) {
|
||||
if (!pluginData.state.pendingRoleChanges.has(memberId)) {
|
||||
const newPendingRoleChangeObj: PendingMemberRoleChanges = {
|
||||
timeout: null,
|
||||
changes: [],
|
||||
applyFn: async () => {
|
||||
pluginData.state.pendingRoleChanges.delete(memberId);
|
||||
|
||||
const lock = await pluginData.locks.acquire(`member-roles-${memberId}`);
|
||||
|
||||
const member = await resolveMember(pluginData.client, pluginData.guild, memberId);
|
||||
if (member) {
|
||||
const newRoleIds = new Set(member.roles);
|
||||
for (const change of newPendingRoleChangeObj.changes) {
|
||||
if (change.mode === "+") newRoleIds.add(change.roleId);
|
||||
else newRoleIds.delete(change.roleId);
|
||||
}
|
||||
|
||||
try {
|
||||
await member.edit(
|
||||
{
|
||||
roles: Array.from(newRoleIds.values()),
|
||||
},
|
||||
"Reaction roles",
|
||||
);
|
||||
} catch (e) {
|
||||
logger.warn(
|
||||
`Failed to apply role changes to ${member.username}#${member.discriminator} (${member.id}): ${e.message}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
lock.unlock();
|
||||
},
|
||||
};
|
||||
|
||||
pluginData.state.pendingRoleChanges.set(memberId, newPendingRoleChangeObj);
|
||||
}
|
||||
|
||||
const pendingRoleChangeObj = pluginData.state.pendingRoleChanges.get(memberId);
|
||||
pendingRoleChangeObj.changes.push({ mode, roleId });
|
||||
|
||||
if (pendingRoleChangeObj.timeout) clearTimeout(pendingRoleChangeObj.timeout);
|
||||
pendingRoleChangeObj.timeout = setTimeout(
|
||||
() => pluginData.state.roleChangeQueue.add(pendingRoleChangeObj.applyFn),
|
||||
ROLE_CHANGE_BATCH_DEBOUNCE_TIME,
|
||||
);
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
import { PluginData } from "knub";
|
||||
import { ReactionRolesPluginType } from "../types";
|
||||
import { ReactionRole } from "src/data/entities/ReactionRole";
|
||||
import { TextChannel } from "eris";
|
||||
import { isDiscordRESTError, sleep, isSnowflake } from "src/utils";
|
||||
import { logger } from "src/logger";
|
||||
|
||||
const CLEAR_ROLES_EMOJI = "❌";
|
||||
|
||||
export async function applyReactionRoleReactionsToMessage(
|
||||
pluginData: PluginData<ReactionRolesPluginType>,
|
||||
channelId: string,
|
||||
messageId: string,
|
||||
reactionRoles: ReactionRole[],
|
||||
) {
|
||||
const channel = pluginData.guild.channels.get(channelId) as TextChannel;
|
||||
if (!channel) return;
|
||||
|
||||
let targetMessage;
|
||||
try {
|
||||
targetMessage = await channel.getMessage(messageId);
|
||||
} catch (e) {
|
||||
if (isDiscordRESTError(e)) {
|
||||
if (e.code === 10008) {
|
||||
// Unknown message, remove reaction roles from the message
|
||||
logger.warn(
|
||||
`Removed reaction roles from unknown message ${channelId}/${messageId} in guild ${pluginData.guild.name} (${pluginData.guild.id})`,
|
||||
);
|
||||
await pluginData.state.reactionRoles.removeFromMessage(messageId);
|
||||
} else {
|
||||
logger.warn(
|
||||
`Error when applying reaction roles to message ${channelId}/${messageId} in guild ${pluginData.guild.name} (${pluginData.guild.id}), error code ${e.code}`,
|
||||
);
|
||||
}
|
||||
|
||||
return;
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
// Remove old reactions, if any
|
||||
const removeSleep = sleep(1250);
|
||||
await targetMessage.removeReactions();
|
||||
await removeSleep;
|
||||
|
||||
// Add reaction role reactions
|
||||
for (const rr of reactionRoles) {
|
||||
const emoji = isSnowflake(rr.emoji) ? `foo:${rr.emoji}` : rr.emoji;
|
||||
|
||||
const sleepTime = sleep(1250); // Make sure we only add 1 reaction per ~second so as not to hit rate limits
|
||||
await targetMessage.addReaction(emoji);
|
||||
await sleepTime;
|
||||
}
|
||||
|
||||
// Add the "clear reactions" button
|
||||
await targetMessage.addReaction(CLEAR_ROLES_EMOJI);
|
||||
}
|
10
backend/src/plugins/ReactionRoles/util/autoRefreshLoop.ts
Normal file
10
backend/src/plugins/ReactionRoles/util/autoRefreshLoop.ts
Normal file
|
@ -0,0 +1,10 @@
|
|||
import { PluginData } from "knub";
|
||||
import { ReactionRolesPluginType } from "../types";
|
||||
import { runAutoRefresh } from "./runAutoRefresh";
|
||||
|
||||
export async function autoRefreshLoop(pluginData: PluginData<ReactionRolesPluginType>, interval: number) {
|
||||
pluginData.state.autoRefreshTimeout = setTimeout(async () => {
|
||||
await runAutoRefresh(pluginData);
|
||||
autoRefreshLoop(pluginData, interval);
|
||||
}, interval);
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
import { ReactionRolesPluginType } from "../types";
|
||||
import { PluginData } from "knub";
|
||||
import { applyReactionRoleReactionsToMessage } from "./applyReactionRoleReactionsToMessage";
|
||||
|
||||
export async function refreshReactionRoles(
|
||||
pluginData: PluginData<ReactionRolesPluginType>,
|
||||
channelId: string,
|
||||
messageId: string,
|
||||
) {
|
||||
const pendingKey = `${channelId}-${messageId}`;
|
||||
if (pluginData.state.pendingRefreshes.has(pendingKey)) return;
|
||||
pluginData.state.pendingRefreshes.add(pendingKey);
|
||||
|
||||
try {
|
||||
const reactionRoles = await pluginData.state.reactionRoles.getForMessage(messageId);
|
||||
await applyReactionRoleReactionsToMessage(pluginData, channelId, messageId, reactionRoles);
|
||||
} finally {
|
||||
pluginData.state.pendingRefreshes.delete(pendingKey);
|
||||
}
|
||||
}
|
13
backend/src/plugins/ReactionRoles/util/runAutoRefresh.ts
Normal file
13
backend/src/plugins/ReactionRoles/util/runAutoRefresh.ts
Normal file
|
@ -0,0 +1,13 @@
|
|||
import { PluginData } from "knub";
|
||||
import { ReactionRolesPluginType } from "../types";
|
||||
import { refreshReactionRoles } from "./refreshReactionRoles";
|
||||
|
||||
export async function runAutoRefresh(pluginData: PluginData<ReactionRolesPluginType>) {
|
||||
// Refresh reaction roles on all reaction role messages
|
||||
const reactionRoles = await pluginData.state.reactionRoles.all();
|
||||
const idPairs = new Set(reactionRoles.map(r => `${r.channel_id}-${r.message_id}`));
|
||||
for (const pair of idPairs) {
|
||||
const [channelId, messageId] = pair.split("-");
|
||||
await refreshReactionRoles(pluginData, channelId, messageId);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue