Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added donator role support to the PostUpdateMemberRoles handler. #21

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
Loading