Skip to content

Commit

Permalink
Added donator role support to the PostUpdateMemberRoles handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
Osvaldon committed Jan 17, 2024
1 parent b44ae1b commit 833a691
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
26 changes: 18 additions & 8 deletions DiscordBot.App/Controllers/DiscordController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,32 @@ public async Task PostUpdateMemberRoles(DiscordMemberRolesDto memberRoles)

foreach (var member in members)
{
var hasVIP = memberRoles.MemberRolesVIP.TryGetValue(member.Id, out var vipId);
var hasVIP = memberRoles.MemberRolesVIP.TryGetValue(member.Id, out var roleVIP);
var hasDonator = memberRoles.MemberRolesDonator.TryGetValue(member.Id, out var roleDonator);

foreach (var role in member.Roles)
{
if (memberRoles.RolesVIP.Contains(role.Id))
if (memberRoles.RolesVIP.Contains(role.Id) && (!hasVIP || role.Id != roleVIP))
{
if (!hasVIP|| role.Id != vipId)
{
await member.RevokeRoleAsync(role);
}
await member.RevokeRoleAsync(role);
}

if (memberRoles.RolesDonator.Contains(role.Id) && (!hasDonator || role.Id != roleDonator))
{
await member.RevokeRoleAsync(role);
}
}

if (hasVIP && !member.Roles.Any((role) => role.Id == roleVIP))
{
var role = _discordService.Guild.GetRole(roleVIP);
if (role != null)
await member.GrantRoleAsync(role);
}

if (hasVIP && !member.Roles.Any((role) => role.Id == vipId))
if (hasDonator && !member.Roles.Any((role) => role.Id == roleDonator))
{
var role = _discordService.Guild.GetRole(vipId);
var role = _discordService.Guild.GetRole(roleDonator);
if (role != null)
await member.GrantRoleAsync(role);
}
Expand Down
21 changes: 19 additions & 2 deletions DiscordBot.Data/Models/DiscordMemberRolesDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ namespace DiscordBot.Data.Models;

public class DiscordMemberRolesDto
{
public HashSet<ulong> RolesVIP { get; set; }
public Dictionary<ulong, ulong> MemberRolesVIP { get; set; }
/// <summary>
/// A collection of VIP Discord role ids.
/// </summary>
public HashSet<ulong> RolesVIP { get; set; } = new();

/// <summary>
/// A collection of VIP Discord member ids and their role ids.
/// </summary>
public Dictionary<ulong, ulong> MemberRolesVIP { get; set; } = new();

/// <summary>
/// A collection of donator Discord role ids.
/// </summary>
public HashSet<ulong> RolesDonator { get; set; } = new();

/// <summary>
/// A collection of donator Discord member ids and their role ids.
/// </summary>
public Dictionary<ulong, ulong> MemberRolesDonator { get; set; } = new();
}

0 comments on commit 833a691

Please sign in to comment.