Skip to content

Commit

Permalink
feat: display error message when role is below highest bot role in /c…
Browse files Browse the repository at this point in the history
…heckroles
  • Loading branch information
tippfehlr committed Aug 23, 2024
1 parent f555666 commit cdca320
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 25 deletions.
6 changes: 5 additions & 1 deletion locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@
"description": "re-check all users/roles",
"success": "checked {{{usersChecked}}} users, added {{{added}}} and removed {{{removed}}} roles",
"in-progress": "IN PROGRESS: already checked {{{usersChecked}}}/{{{totalUsersToCheck}}} users, added {{{added}}} and removed {{{removed}}} roles.",
"askForManageRolesPermission": "I don’t have the `MANAGE_ROLES` permission but need it to assign roles.\n Please add the permission to the “Activity Roles” role. Otherwise the bot will leave the guild automatically."
"askForManageRolesPermission": "I don’t have the `MANAGE_ROLES` permission but need it to assign roles.\n Please add the permission to the “Activity Roles” role. Otherwise the bot will leave the guild automatically.",
"alreadyRunning": "A `/checkroles` request is already running for this guild."
},
"Cancelled": "Cancelled",
"deleteActivityRole": {
Expand All @@ -118,5 +119,8 @@
},
"deleteActivityRoles": {
"deleteAllConfirmation": "Are you sure you want to delete all activity roles?"
},
"presenceUpdate": {
"roleHigherThanBotRole": "I don’t have permission to manage %s because it is higher than my highest role.\nPlease move one of my roles above all Activity Roles."
}
}
39 changes: 30 additions & 9 deletions src/modules/bot.presenceUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import {
Activity,
ActivityType,
CommandInteraction,
Events,
Guild,
GuildMember,
Expand All @@ -18,8 +19,9 @@ import {
db,
hashUserID,
roleRemoved,
getLang,
} from './db';
import { log } from './messages';
import { log, __ } from './messages';
import { writeIntPoint } from './metrics';
import { client, stats } from './bot';
import { Selectable } from 'kysely';
Expand Down Expand Up @@ -59,23 +61,36 @@ export async function addDiscordRoleToMember({
permanent,
guild,
member,
interaction,
}: {
change: 'add' | 'remove';
roleID: string;
permanent?: boolean;
guild: Guild;
member: GuildMember;
}): Promise<addRoleStatus | undefined> {
interaction?: CommandInteraction;
}): Promise<addRoleStatus | 'abort' | undefined> {
const role = guild.roles.cache.get(roleID);
if (!role) {
roleRemoved(roleID, guild.id);
return;
}
const highestBotRolePosition = guild.members.me?.roles.highest.position;
if (!highestBotRolePosition || highestBotRolePosition <= role.position) {
log.warn(
`Role ${role.name} is higher than the bot’s highest role and was skipped (in guild ${guild.name})`,
);
if (interaction) {
interaction.editReply({
content: __(
{ phrase: 'presenceUpdate->roleHigherThanBotRole', locale: getLang(interaction) },
`<@&${role.id}>`,
),
});
return 'abort';
} else {
log.warn(
`Role ${role.name} is higher than the bot’s highest role and was \
skipped (in guild ${guild.name})`,
);
}
return;
}

Expand Down Expand Up @@ -127,6 +142,7 @@ export async function processRoles({
activeTemporaryRoles,
guild,
member,
interaction,
}: {
memberStatus: PresenceStatus;
statusRoles: Selectable<StatusRoles>[];
Expand All @@ -135,7 +151,8 @@ export async function processRoles({
guild: Guild;
member: GuildMember;
activeTemporaryRoles: Selectable<ActiveTemporaryRoles>[];
}): Promise<{ added: number; removed: number }> {
interaction?: CommandInteraction;
}): Promise<{ added: number; removed: number } | 'abort'> {
const status = { added: 0, removed: 0 };
if (member.user.bot) return status;

Expand All @@ -157,6 +174,7 @@ export async function processRoles({
change,
guild,
member,
interaction,
})
) {
case addRoleStatus.RoleAdded:
Expand All @@ -165,6 +183,8 @@ export async function processRoles({
case addRoleStatus.RoleRemoved:
status.removed++;
break;
case 'abort':
return 'abort';
}
};
// if user is offline, skip checking for added activities
Expand Down Expand Up @@ -198,17 +218,18 @@ export async function processRoles({
// ------------ “apply changes” ------------

for (const roleID of permanentRoleIDsToBeAdded) {
await addRoleHelper(roleID, 'add', true);
if ((await addRoleHelper(roleID, 'add', true)) === 'abort') return 'abort';
}
for (const roleID of tempRoleIDsToBeAdded) {
await addRoleHelper(roleID, 'add', false);
if ((await addRoleHelper(roleID, 'add', false)) === 'abort') return 'abort';
}
}

// remove temporary roles --- new activeTemporaryRoles
for (const activeTemporaryRole of activeTemporaryRoles) {
if (!tempRoleIDsToBeAdded.has(activeTemporaryRole.roleID)) {
await addRoleHelper(activeTemporaryRole.roleID, 'remove', false);
if ((await addRoleHelper(activeTemporaryRole.roleID, 'remove', false)) === 'abort')
return 'abort';
}
}
return status;
Expand Down
30 changes: 15 additions & 15 deletions src/modules/commands/checkRoles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,13 @@ export async function checkRolesStandalone({
if (!guild.members.me?.permissions.has(PermissionsBitField.Flags.ManageRoles)) {
if (interaction && locale) {
await interaction.reply({
content: __({
phrase:
'checkRoles->askForManageRolesPermission:I don’t have the `MANAGE_ROLES` permission but need it to assign roles.\n\
Please add the permission to the “Activity Roles” role. Otherwise the bot will leave the guild automatically.',
}),
content: __({ phrase: 'checkRoles->askForManageRolesPermission', locale }),
});
return;
} else {
log.warn(
`MISSING ACCESS: LEFT guild: ${guild.name} (ID: ${guild.id}, OwnerID: ${guild.ownerId}), Permission: MANAGE_ROLES`,
`MISSING ACCESS: LEFT guild: ${guild.name} (ID: ${guild.id}, OwnerID: \
${guild.ownerId}), Permission: MANAGE_ROLES`,
);
await guild.leave();
return;
Expand All @@ -57,11 +54,7 @@ Please add the permission to the “Activity Roles” role. Otherwise the bot wi
log.debug(`checkroles already running on ${guild.name} (${guild.id})`);
if (interaction && locale) {
await interaction.reply({
content: __({
phrase:
'checkRoles->alreadyRunning:A `/checkroles` request is already running for this guild.',
locale,
}),
content: __({ phrase: 'checkRoles->alreadyRunning', locale }),
ephemeral: true,
});
}
Expand Down Expand Up @@ -122,10 +115,7 @@ Please add the permission to the “Activity Roles” role. Otherwise the bot wi
interval = setInterval(() => {
interaction.editReply({
content: __(
{
phrase: `checkRoles->in-progress:IN PROGRESS: already checked {{{usersChecked}}}/{{{totalUsersToCheck}}} users, added {{{added}}} and removed {{{removed}}} roles.`,
locale,
},
{ phrase: 'checkRoles->in-progress', locale },
{
usersChecked: status.usersChecked.toString(),
added: status.rolesAdded.toString(),
Expand Down Expand Up @@ -155,7 +145,17 @@ Please add the permission to the “Activity Roles” role. Otherwise the bot wi
activeTemporaryRoles,
guild,
member: presence.member,
interaction,
});
// res can only be 'abort' when interaction is passed.
// check for this nonetheless because it could change in the future.
if (res === 'abort') {
if (interaction && locale) {
clearInterval(interval);
}
checkrolesCurrentGuilds.delete(guild.id);
return;
}
status.rolesAdded += res.added;
status.rolesRemoved += res.removed;

Expand Down

0 comments on commit cdca320

Please sign in to comment.