Moved EntryRole to self-hosted

As it's currently implemented, it may cause problems for larger guilds
if any error is encountered. Additionally, it requires that the bot keep
tabs on all users on entry which may bring unnecessary extra load on the
host.

Additionally, added some error handling.
This commit is contained in:
Noikoio 2019-06-20 17:35:50 -07:00
parent 90feeb47b0
commit c1e8f6699a
2 changed files with 30 additions and 10 deletions

View file

@ -3,6 +3,7 @@ using Newtonsoft.Json.Linq;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -107,21 +108,40 @@ namespace Kerobot.Modules.EntryRole
var targetRole = gconf.TargetRole.FindRoleIn(g, true); var targetRole = gconf.TargetRole.FindRoleIn(g, true);
if (targetRole == null) if (targetRole == null)
{ {
// Notify of this failure. await ReportFailure(g.Id, "Unable to determine role to be applied. Does it still exist?", gusers);
string failList = ""; return;
foreach (var item in gusers) failList += $", {item.Username}#{item.Discriminator}";
await LogAsync(g.Id, "Unable to find role to apply. (Was the role deleted?) " +
"Failed to set role to the following users: " + failList.Substring(2));
} }
// Apply roles // Apply roles
foreach (var item in gusers) try
{ {
// TODO exception handling and notification on forbidden foreach (var item in gusers)
if (item.Roles.Contains(targetRole)) continue; {
await item.AddRoleAsync(targetRole); if (item.Roles.Contains(targetRole)) continue;
await item.AddRoleAsync(targetRole);
}
} }
catch (Discord.Net.HttpException ex) when (ex.HttpCode == System.Net.HttpStatusCode.Forbidden)
{
await ReportFailure(g.Id, "Unable to set role due to a permissions issue.", gusers);
}
}
private async Task ReportFailure(ulong gid, string message, IEnumerable<SocketGuildUser> failedUserList)
{
var failList = new StringBuilder();
var count = 0;
foreach (var item in failedUserList) {
failList.Append($", {item.Username}#{item.Discriminator}");
count++;
if (count > 5)
{
failList.Append($"and {count} other(s).");
break;
}
}
failList.Remove(0, 2);
await LogAsync(gid, message + " Failed while attempting to set role on the following users: " + failList.ToString());
} }
} }
} }