Skip to content

Commit

Permalink
Merge pull request #10 from murl-digital/development
Browse files Browse the repository at this point in the history
1.0.8
  • Loading branch information
murl-digital committed Apr 1, 2021
2 parents d0f9706 + 715170c commit 9403ac0
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 54 deletions.
7 changes: 1 addition & 6 deletions .idea/.idea.GarbageCan/.idea/indexLayout.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion GarbageCan/GarbageCan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal static class GarbageCan
private static IFeature[] _botFeatures;
private static bool _shutdown;

public static ulong operatingGuildId
public static ulong OperatingGuildId
{
get
{
Expand Down
4 changes: 2 additions & 2 deletions GarbageCan/GarbageCan.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<ItemGroup>
<PackageReference Include="Config.Net" Version="4.15.0" />
<PackageReference Include="Config.Net.Json" Version="4.15.0" />
<PackageReference Include="DSharpPlus" Version="4.0.0-nightly-00816" />
<PackageReference Include="DSharpPlus.CommandsNext" Version="4.0.0-nightly-00816" />
<PackageReference Include="DSharpPlus" Version="4.0.0-rc2" />
<PackageReference Include="DSharpPlus.CommandsNext" Version="4.0.0-rc2" />
<PackageReference Include="Humanizer.Core" Version="2.8.26" />
<PackageReference Include="MathNet.Numerics" Version="4.15.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.4" />
Expand Down
10 changes: 5 additions & 5 deletions GarbageCan/Moderation/ModManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class ModManager : IFeature
private static DiscordRole _mutedRole;
private static readonly Timer Timer = new(TimeSpan.FromMinutes(1).TotalMilliseconds);

private static ulong mutedRoleId
private static ulong MutedRoleId
{
get
{
Expand All @@ -31,7 +31,7 @@ public void Init(DiscordClient client)
{
client.GuildDownloadCompleted += (sender, _) =>
{
_mutedRole = sender.Guilds[GarbageCan.operatingGuildId].GetRole(mutedRoleId);
_mutedRole = sender.Guilds[GarbageCan.OperatingGuildId].GetRole(MutedRoleId);
Timer.Enabled = true;
return Task.CompletedTask;
};
Expand Down Expand Up @@ -178,7 +178,7 @@ await context.moderationActiveMutes
.Where(m => m.expirationDate <= now)
.ForEachAsync(async m =>
{
var member = await GarbageCan.Client.Guilds[GarbageCan.operatingGuildId]
var member = await GarbageCan.Client.Guilds[GarbageCan.OperatingGuildId]
.GetMemberAsync(m.uId);
await member.RevokeRoleAsync(_mutedRole, "mute expired");
});
Expand Down Expand Up @@ -206,9 +206,9 @@ await context.moderationActiveChannelRestricts
.Where(c => c.expirationDate <= now)
.ForEachAsync(async c =>
{
var member = await GarbageCan.Client.Guilds[GarbageCan.operatingGuildId]
var member = await GarbageCan.Client.Guilds[GarbageCan.OperatingGuildId]
.GetMemberAsync(c.uId);
var channel = GarbageCan.Client.Guilds[GarbageCan.operatingGuildId]
var channel = GarbageCan.Client.Guilds[GarbageCan.OperatingGuildId]
.GetChannel(c.channelId);
await channel.PermissionOverwrites.First(o => o.Id == member.Id)
Expand Down
2 changes: 1 addition & 1 deletion GarbageCan/Moderation/ModUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static class ModUtil
{
public static async void SendMessage(ulong uId, string message)
{
var member = await GarbageCan.Client.Guilds[GarbageCan.operatingGuildId].GetMemberAsync(uId);
var member = await GarbageCan.Client.Guilds[GarbageCan.OperatingGuildId].GetMemberAsync(uId);
try
{
try
Expand Down
85 changes: 47 additions & 38 deletions GarbageCan/Roles/RoleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Timers;
using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.EventArgs;
Expand All @@ -16,6 +17,8 @@ namespace GarbageCan.Roles
{
public class RoleManager : IFeature
{
private readonly Timer _roleTaskTimer = new(5000);

public void Init(DiscordClient client)
{
XpManager.GhostLevelUp += HandleLevelRoles;
Expand All @@ -33,7 +36,14 @@ public void Init(DiscordClient client)
return Task.CompletedTask;
};
client.GuildMemberUpdated += HandleJoinRoles;
client.GuildMemberUpdated += HandleConditionalRoles;

client.Ready += (_, _) =>
{
_roleTaskTimer.Elapsed += Tick;
_roleTaskTimer.Enabled = true;
return Task.CompletedTask;
};
}

public void Cleanup()
Expand All @@ -50,7 +60,7 @@ private static Task ReactionAdded(DiscordClient sender, MessageReactionAddEventA
{
try
{
using var context = new Context();
await using var context = new Context();
await context.reactionRoles.ForEachAsync(async r =>
{
try
Expand Down Expand Up @@ -135,9 +145,9 @@ await context.joinRoles.ForEachAsync(async r =>
var role = e.Guild.GetRole(r.roleId);
await e.Member.GrantRoleAsync(role, "join role");
}
catch (Exception e)
catch (Exception ex)
{
Log.Error(e, "couldn't grant role to user");
Log.Error(ex, "couldn't grant role to user");
}
});
}
Expand Down Expand Up @@ -191,61 +201,60 @@ private static void HandleLevelRoles(object sender, XpEventArgs args)
});
}

private static Task HandleConditionalRoles(DiscordClient sender, GuildMemberUpdateEventArgs e)
private static void Tick(object sender, ElapsedEventArgs elapsedEventArgs)
{
if (e.Member.IsBot) return Task.CompletedTask;
if (e.RolesBefore.Count == e.RolesAfter.Count) return Task.CompletedTask;

Task.Run(async () =>
{
try
{
await using var context = new Context();
var conditionalRoles = new Dictionary<ulong, List<ulong>>();
var conditionalRolesEntity = await context.conditionalRoles.ToArrayAsync();
var members = await GarbageCan.Client.Guilds[GarbageCan.OperatingGuildId].GetAllMembersAsync();
var taskList = new List<Task>();
if (e.RolesBefore.Count < e.RolesAfter.Count)
foreach (var role in conditionalRolesEntity)
{
var roles = e.RolesAfter.Except(e.RolesBefore);
foreach (var role in roles)
if (!conditionalRoles.TryGetValue(role.resultRoleId, out var requiredRoles))
{
if (!context.conditionalRoles.Any(r => r.requiredRoleId == role.Id)) continue;
await context.conditionalRoles.Where(r => r.requiredRoleId == role.Id).ForEachAsync(r =>
{
var toBeAssigned = e.Guild.GetRole(r.resultRoleId);
e.Member.GrantRoleAsync(toBeAssigned);
});
requiredRoles = new List<ulong>();
conditionalRoles.Add(role.resultRoleId, requiredRoles);
}
requiredRoles.Add(role.requiredRoleId);
}
if (e.RolesBefore.Count > e.RolesAfter.Count)
var keySet = conditionalRoles.Keys;
foreach (var member in members)
{
var roles = e.RolesBefore.Except(e.RolesAfter);
foreach (var role in member.Roles)
{
if (keySet.All(k => k != role.Id)) continue;
if (!member.Roles.Select(r => r.Id).Any(r => conditionalRoles[role.Id].Any(i => i == r)))
{
if (conditionalRolesEntity.First(r => r.resultRoleId == role.Id).remain) continue;
taskList.Add(member.RevokeRoleAsync(role, "conditional roles"));
}
}
foreach (var role in roles)
var roles = member.Roles.Select(r => r.Id).ToArray();
foreach (var (key, value) in conditionalRoles)
{
if (!context.conditionalRoles.Any(r => r.requiredRoleId == role.Id)) continue;
if (context.conditionalRoles.Count(r =>
r.resultRoleId == role.Id &&
e.RolesAfter.Select(d => d.Id).Contains(r.requiredRoleId)) > 1)
continue;
await context.conditionalRoles.Where(r => r.requiredRoleId == role.Id && !r.remain)
.ForEachAsync(r =>
{
var toBeRemoved = e.Guild.GetRole(r.resultRoleId);
e.Member.RevokeRoleAsync(toBeRemoved);
});
if (roles.Any(r => value.Any(i => i == r)) && roles.All(r => r != key))
{
taskList.Add(member.GrantRoleAsync(member.Guild.GetRole(key), "conditional roles"));
}
}
}
await Task.WhenAll(taskList);
}
catch (Exception ex)
catch (Exception e)
{
Log.Error(ex, "uh oh");
Log.Error(e, "Conditional role update failed");
}
});

return Task.CompletedTask;
}
}
}
2 changes: 1 addition & 1 deletion GarbageCan/XP/Boosters/BoosterManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void Init(DiscordClient client)

client.GuildDownloadCompleted += (_, args) =>
{
_nitroBoosterCount = args.Guilds[GarbageCan.operatingGuildId].PremiumSubscriptionCount ??
_nitroBoosterCount = args.Guilds[GarbageCan.OperatingGuildId].PremiumSubscriptionCount ??
_nitroBoosterCount;
return Task.CompletedTask;
Expand Down

0 comments on commit 9403ac0

Please sign in to comment.