Skip to content

Commit

Permalink
oops
Browse files Browse the repository at this point in the history
  • Loading branch information
SylveonDeko committed Dec 11, 2023
1 parent ba0fe17 commit 2c500a4
Showing 1 changed file with 98 additions and 42 deletions.
140 changes: 98 additions & 42 deletions src/Mewdeko/Modules/StatusRoles/Services/StatusRolesService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class StatusRolesService : INService, IReadyExecutor
private readonly DiscordSocketClient client;
private readonly DbService db;
private readonly IDataCache cache;
private readonly HashSet<StatusRolesTable> statusRoles = [];
private readonly HashSet<StatusRolesTable> statusRoles = new();

public StatusRolesService(DiscordSocketClient client, DbService db, EventHandler eventHandler, IDataCache cache)
{
Expand All @@ -34,100 +34,156 @@ private async Task EventHandlerOnPresenceUpdated(SocketUser args, SocketPresence
{
try
{
// Early exit if there are no status roles
if (this.statusRoles.Count == 0)
return;

// Ensure the incoming user is a guild user
if (args is not SocketGuildUser user)
return;

// Get the status of the user before and after the event
var beforeStatus = args2?.Activities?.FirstOrDefault() as CustomStatusGame;
var afterStatus = args3.Activities?.FirstOrDefault() as CustomStatusGame;
if (args3.Activities?.FirstOrDefault() is not CustomStatusGame status)
{
return;
}

// Continue only if the event is non-null and status has changed
if (afterStatus == null || afterStatus.State == beforeStatus?.State)
if (status.State is null && beforeStatus?.State is null || status.State == beforeStatus?.State)
{
return;
}

// Update user status in cache only if status has changed
var newStatusBase64 = afterStatus.State?.ToBase64() ?? "none";
if (!await cache.SetUserStatusCache(args.Id, newStatusBase64))
if (!await cache.SetUserStatusCache(args.Id,
status.State?.ToBase64() is null ? "none" : status.State.ToBase64()))
{
return;
}

// Check for any statusRoles, if there are none, no further actions needed
await using var uow = db.GetDbContext();
if (statusRoles.Count == 0)
{
return;
}

// Fetch role ids of user to local variable
var userRoleIds = user.Roles.Select(x => x.Id).ToList();

// Filter statusRoles for a particular guild
var statusRolesTables = statusRoles.Where(x => x.GuildId == user.Guild.Id);
var statusRolesTables = statusRoles.Where(x => x.GuildId == user.Guild.Id).ToList();

// Loops through each status role in the guild
foreach (var config in statusRolesTables)
foreach (var i in statusRolesTables)
{
var toAdd = string.IsNullOrWhiteSpace(config.ToAdd)
? new List<ulong>()
: config.ToAdd.Split(" ").Select(ulong.Parse).Where(role => !userRoleIds.Contains(role)).ToList();
var toAdd = new List<ulong>();
var toRemove = new List<ulong>();
if (!string.IsNullOrWhiteSpace(i.ToAdd))
toAdd = i.ToAdd.Split(" ").Select(ulong.Parse).ToList();
if (!string.IsNullOrWhiteSpace(i.ToRemove))
toRemove = i.ToRemove.Split(" ").Select(ulong.Parse).ToList();
if (status.State is null || !status.State.Contains(i.Status))
{
if (beforeStatus is not null && beforeStatus.State.Contains(i.Status))
{
if (i.RemoveAdded == 1)
{
if (toAdd.Count != 0)
{
foreach (var role in toAdd.Where(socketRole =>
user.Roles.Select(x => x.Id).Contains(socketRole)))
{
try
{
await user.RemoveRoleAsync(role);
}
catch
{
Log.Error(
"Unable to remove added role {Role} for {User} in {UserGuild} due to permission issues",
role, user, user.Guild);
}
}
}
}

var toRemove = string.IsNullOrWhiteSpace(config.ToRemove)
? new List<ulong>()
: config.ToRemove.Split(" ").Select(ulong.Parse).Where(role => userRoleIds.Contains(role)).ToList();
if (i.ReaddRemoved == 1)
{
if (toRemove.Count != 0)
{
foreach (var role in toRemove.Where(socketRole =>
!user.Roles.Select(x => x.Id).Contains(socketRole)))
{
try
{
await user.AddRoleAsync(role);
}
catch
{
Log.Error(
$"Unable to add removed role {role} for {user} in {user.Guild} due to permission issues.");
}
}
}
}
}
else
{
continue;
}
}

var channel = user.Guild.GetTextChannel(config.StatusChannelId);
// If the StatusEmbed field is empty or channel is null, continue to the next statusRole
if (string.IsNullOrWhiteSpace(config.StatusEmbed) || channel == null)
if (beforeStatus is not null && beforeStatus.State.Contains(i.Status))
{
continue;
}

if (afterStatus.State.Contains(config.Status) && beforeStatus?.State != afterStatus.State)
if (toRemove.Count != 0)
{
// On Status Add
try
{
await user.AddRolesAsync(toAdd);
await user.RemoveRolesAsync(toRemove);
}
catch
{
Log.Error($"Unable to add status roles in {user.Guild} due to permission issues.");
Log.Error($"Unable to remove statusroles in {user.Guild} due to permission issues.");
}
}
else if ((beforeStatus?.State.Contains(config.Status) ?? false) &&
beforeStatus?.State != afterStatus.State)

if (toAdd.Any())
{
// On Status Remove
try
{
await user.RemoveRolesAsync(toRemove);
await user.AddRolesAsync(toAdd);
}
catch
{
Log.Error($"Unable to remove status roles in {user.Guild} due to permission issues.");
Log.Error($"Unable to add statusroles in {user.Guild} due to permission issues.");
}
}

var channel = user.Guild.GetTextChannel(i.StatusChannelId);

if (channel is null)
{
continue;
}

if (string.IsNullOrWhiteSpace(i.StatusEmbed))
{
continue;
}

var rep = new ReplacementBuilder().WithDefault(user, channel, user.Guild, client).Build();

if (SmartEmbed.TryParse(rep.Replace(config.StatusEmbed), user.Guild.Id, out var embeds,
out var plainText,
if (SmartEmbed.TryParse(rep.Replace(i.StatusEmbed), user.Guild.Id, out var embeds, out var plainText,
out var components))
{
await channel.SendMessageAsync(plainText ?? null, embeds: embeds ?? Array.Empty<Embed>(),
components: components?.Build());
}
else
{
await channel.SendMessageAsync(rep.Replace(config.StatusEmbed));
await channel.SendMessageAsync(rep.Replace(i.StatusEmbed));
}
}
}
catch (Exception e)
{
var status = args3.Activities?.FirstOrDefault() as CustomStatusGame;
Log.Error(
"Error in StatusRolesService: {Exception}, After Status: {Status}, args2: {Args2}, args3: {Args3}",
e, status?.State, args2, args3);
Log.Error("Error in StatusRolesService. After Status: {Status} args: {Args2} args2: {Args3}\n{Exception}",
status.State, args2, args3, e);
}
}

Expand Down

0 comments on commit 2c500a4

Please sign in to comment.