These are the methods. Error occurs on the GrantRoleAsync line.
AddActiveRoleCommand is part of a CommandModule.
AddActiveRoleToAll is in a helper.
AddActiveRole is in the same helper.
public async Task AddActiveRole(DiscordGuild server, DiscordMember discordMember)
{
if (server == null) { return; }
if (discordMember == null) { return; }
if (!string.Equals(server.Name, SERVER_NAME, StringComparison.OrdinalIgnoreCase)) { return; }
DiscordRole activeRole = server.Roles.Values.FirstOrDefault(role => string.Equals(role.Name, ACTIVE_ROLE, StringComparison.OrdinalIgnoreCase));
if (activeRole == null) { return; }
IEnumerable<DiscordRole> memberRoles = discordMember.Roles;
if (memberRoles == null) { return; }
bool hasRole = memberRoles.Any(role => string.Equals(role.Name, activeRole.Name, StringComparison.OrdinalIgnoreCase));
if (hasRole) { return; }
try
{
await discordMember.GrantRoleAsync(activeRole);
}
catch (NotFoundException e) { }
}```
`AddActiveRole` is called from `AddActiveRoleToAll`
```cs
public async Task<int> AddActiveRoleToAll(DiscordGuild server)
{
List<ulong> activeIds = new List<ulong>();
foreach (DiscordChannel channel in server.Channels.Values)
{
if (channel.IsCategory) { continue; }
IReadOnlyList<DiscordMessage> messages = await channel.GetMessagesAsync(limit: 500);
for (int i = 0, end = messages.Count; i < end; i++)
{
DiscordUser author = messages[i].Author;
ulong id = author.Id;
if (activeIds.Contains(id)) { continue; }
activeIds.Add(id);
DiscordMember discordMember = await server.GetMemberAsync(id,updateCache: true);
await AddActiveRole(server, discordMember);
}
}
return activeIds.Count;
}```
`AddActiveRoleToAll` is called from `AddActiveRoleCommand`
```cs
[Command("AddActiveRole")]
[Description("Adds the \"active\" role to anyone on the server that has ever said anything.")]
[RequireUserPermissions(Permissions.UseApplicationCommands)]
public async Task AddActiveRoleCommand(CommandContext context)
{
await context.Channel.SendMessageAsync("Adding role to active users...");
DiscordHelper helper = new DiscordHelper();
int added = await helper.AddActiveRoleToAll(context.Guild);
await context.Channel.SendMessageAsync("Role added to " + added + " users.");
}```
`AddActiveRoleCommand` is called from within a channel on a server using a bot command.