Skip to content

Commit

Permalink
guild_settings restore_roles
Browse files Browse the repository at this point in the history
  • Loading branch information
OoLunar committed Oct 14, 2024
1 parent d396355 commit d1c8026
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Threading.Tasks;
using DSharpPlus.Commands;
using DSharpPlus.Commands.ContextChecks;
using DSharpPlus.Entities;
using OoLunar.Tomoe.Database.Models;

namespace OoLunar.Tomoe.Commands.Moderation
{
public sealed partial class GuildSettingsCommand
{
/// <summary>
/// Whether or not to restore roles when a member rejoins the server.
/// </summary>
/// <param name="restoreRoles">Whether or not to restore roles when a member rejoins the server.</param>
[Command("restore_roles")]
[RequirePermissions(DiscordPermissions.ManageRoles, DiscordPermissions.ManageGuild)]
public static async ValueTask RestoreRolesAsync(CommandContext context, bool restoreRoles = false)
{
GuildSettingsModel? settings = await GuildSettingsModel.GetSettingsAsync(context.Guild!.Id);
if (settings is null)
{
await context.RespondAsync(NOT_SETUP_TEXT);
return;
}

await GuildSettingsModel.UpdateSettingsAsync(settings with
{
RestoreRoles = restoreRoles
});

await context.RespondAsync("I will " + (restoreRoles ? "now restore" : "no longer restore") + " a member's previous roles when they rejoin server.");
}
}
}
2 changes: 1 addition & 1 deletion src/Database/Models/GuildMemberModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ ON CONFLICT (user_id, guild_id) DO UPDATE
_countMembersWithRole.Parameters.Add(new("@guild_id", NpgsqlDbType.Bigint));
_countMembersWithRole.Parameters.Add(new("@role_id", NpgsqlDbType.Bigint));

_countMembersOfGuild = new("SELECT COUNT(guild_id) FROM guild_members WHERE guild_id = @guild_id;");
_countMembersOfGuild = new("SELECT COUNT(guild_id) FROM guild_members WHERE guild_id = @guild_id AND state & 1 = 0;");
_countMembersOfGuild.Parameters.Add(new("@guild_id", NpgsqlDbType.Bigint));

_findMutualGuild = new("SELECT guild_id FROM guild_members WHERE user_id = @user_id AND state | 1 = 1;");
Expand Down
9 changes: 6 additions & 3 deletions src/Events/Handlers/GuildMemberEventHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,12 @@ public async Task HandleEventAsync(DiscordClient sender, GuildMemberAddedEventAr

try
{
// Assign roles all at once instead of one at a time for ratelimiting purposes.
// TODO: Configuration setting
await eventArgs.Member.ReplaceRolesAsync(assignedRoles);
if (await GuildSettingsModel.GetRestoreRolesAsync(eventArgs.Guild.Id))
{
// Assign roles all at once instead of one at a time for ratelimiting purposes.
await eventArgs.Member.ReplaceRolesAsync(assignedRoles);
}

guildMemberModel.RoleIds = assignedRoles.Select(x => x.Id).ToList();
}
catch (Exception error)
Expand Down

0 comments on commit d1c8026

Please sign in to comment.