public async Task <uint> UpdateRoleNames(IEnumerable <AppDiscordRole> roles) { var userRoles = await _userRoleRepo.GetRoleList(); uint updatedRoleCount = 0; foreach (var userRole in userRoles) { var discordRole = roles.SingleOrDefault(r => r.Id == userRole.RoleId); if (discordRole == null) { await _discordErrorLogger.LogDiscordError($"Could not find discord role for user role {userRole.Name} ({userRole.RoleId}). Skipping."); continue; } if (!string.Equals(discordRole.Name, userRole.Name, StringComparison.OrdinalIgnoreCase)) { await _userRoleRepo.UpdateRole(userRole.Id, discordRole.Name); updatedRoleCount++; } } return(updatedRoleCount); }
private async Task <uint> GetAwardReactionCount(DiscordMessage message, DiscordMember messageAuthor) { uint awardReactionCount = 0; try { var cookieEmoji = DiscordEmoji.FromUnicode(EmojiMap.Cookie); var cookieReactionUsers = await message.GetReactionsAsync(cookieEmoji); if (cookieReactionUsers != null) { var skipAuthor = false; #if DEBUG skipAuthor = true; #endif awardReactionCount = (uint)cookieReactionUsers .Select(u => u.Id) .Distinct() .Count(x => skipAuthor || x != messageAuthor.Id); } } catch (Exception ex) { await _discordErrorLogger.LogDiscordError(ex.ToString()); } return(awardReactionCount); }
public async Task <DiscordChannel?> ResolveChannel(DiscordGuild guild, ulong channelId) { guild.Channels.TryGetValue(channelId, out var discordChannel); if (discordChannel == null) { try { discordChannel = guild.GetChannel(channelId); } catch (Exception ex) { await _discordErrorLogger.LogDiscordError(ex.ToString()); return(null); } } return(discordChannel); }
private async Task OnMessageReactionAdded(DiscordClient client, MessageReactionAddEventArgs eventArgs) { var channel = eventArgs.Channel; var message = eventArgs.Message; var user = eventArgs.User; var emoji = eventArgs.Emoji; try { if (!ShouldHandleReaction(channel, user)) { return; } var isAwardEmoji = emoji.Name == EmojiMap.Cookie; if (!IsAwardAllowedChannel(channel)) { return; } if (isAwardEmoji) { _awardQueue.Enqueue(new MessageAwardQueueItem(message, MessageAwardQueueAction.ReactionChanged)); } } catch (Exception ex) { _logger.LogError(ex, nameof(OnMessageReactionAdded)); var eventContextError = new EventErrorContext() { EventName = nameof(OnMessageReactionAdded), User = eventArgs.User, Channel = eventArgs.Channel, Guild = eventArgs.Guild }; await _discordErrorLogger.LogDiscordError(eventContextError, ex.ToString()); } }
/// <summary> /// Try to find a suitable error message to return to the user. /// Log error to discord logger /// </summary> /// <param name="sender"></param> /// <param name="e"></param> /// <returns></returns> private async Task OnCommandErrored(CommandsNextExtension sender, CommandErrorEventArgs e) { var ctx = e.Context; var message = ctx.Message; var exception = e.Exception; var commandPrefix = _options.CommandPrefix; var commandHelpText = $"Type \"{commandPrefix}help\" to get some help."; string errorMessage = string.Empty; // Flag unknown commands and not return any error message in this case // as it's easy for users to accidentally trigger commands using the prefix bool isUnknownCommand = false; bool appendHelpText = false; const string unknownSubcommandErrorString = "No matching subcommands were found, and this group is not executable."; const string unknownOverloadErrorString = "Could not find a suitable overload for the command."; var isChecksFailedException = exception is ChecksFailedException; var isUnknownCommandException = exception is CommandNotFoundException; var isUnknownSubcommandException = exception.Message == unknownSubcommandErrorString; var isUnknownOverloadException = exception.Message == unknownOverloadErrorString; var isCommandConfigException = exception is DuplicateCommandException || exception is DuplicateOverloadException || exception is InvalidOverloadException; // TODO: If this isn't enough, create a custom exception class for validation errors var isPossiblyValidationException = exception is ArgumentException; if (isUnknownCommandException) { errorMessage = $"I do not recognize your command."; isUnknownCommand = true; appendHelpText = true; } else if (isUnknownSubcommandException) { errorMessage = $"I do not recognize your command."; appendHelpText = true; } else if (isUnknownOverloadException) { errorMessage = $"Command arguments are (probably) incorrect."; appendHelpText = true; } else if (isCommandConfigException) { errorMessage = $"Something's not quite right."; appendHelpText = true; } else if (isChecksFailedException) { var checksFailedException = (ChecksFailedException)exception; var failedCheck = checksFailedException.FailedChecks[0]; if (failedCheck is BaseCommandCheck) { errorMessage = "I do not care for DM commands."; } else if (failedCheck is RequireOwnerOrAdmin) { errorMessage = "You do not have permission to do that."; } else { errorMessage = "Preexecution check failed."; } } else if (isPossiblyValidationException) { errorMessage = $"{exception.Message}."; appendHelpText = true; } if (!isUnknownCommand) { if (string.IsNullOrWhiteSpace(errorMessage)) { errorMessage = "Something went wrong."; } if (appendHelpText) { errorMessage += $" {commandHelpText}"; } await message.RespondAsync(errorMessage); } // Log any unhandled exception var shouldLogDiscordError = !isUnknownCommandException && !isUnknownSubcommandException && !isCommandConfigException && !isChecksFailedException && !isPossiblyValidationException; if (shouldLogDiscordError) { await _discordErrorLogger.LogDiscordError(ctx, exception.ToString()); } _logger.LogWarning($"Message: {message.Content}\r\nCommand failed: {exception})"); }