public async Task ReportAsync(Exception e, IDiscordContext context, bool friendlyReply = true, CancellationToken cancellationToken = default) { try { // handle permission exceptions differently if (e is HttpException httpException && httpException.DiscordCode == 50013) // 500013 missing perms { await ReportMissingPermissionAsync(context, cancellationToken); return; } // send error message to the current channel if (friendlyReply) { await _interactiveManager.SendInteractiveAsync( new ErrorMessage(e), context, cancellationToken); } // send detailed error message to the guild error channel var errorChannel = _discord.GetGuild(_settings.Discord.Guild.GuildId) ?.GetTextChannel(_settings.Discord.Guild.ErrorChannelId); if (errorChannel != null && !_environment.IsDevelopment()) { await _interactiveManager.SendInteractiveAsync( new ErrorMessage(e, true), new DiscordContextWrapper(context) { Channel = errorChannel }, cancellationToken); } // send to logger if no error channel or we are debugging else { _logger.LogWarning(e, "Exception while handling message {0}.", context.Message?.Id); } } catch (Exception reportingException) { // ignore reporting errors _logger.LogWarning(reportingException, "Failed to report exception: {0}", e); } }
public override async Task <bool> RunAsync(CancellationToken cancellationToken = default) { if (!await base.RunAsync(cancellationToken) || !DoujinMessage.TryParseDoujinIdFromMessage(Context.Message, out var id, out var isFeed)) { return(false); } var doujin = await _database.GetDoujinAsync(id.source, id.id, cancellationToken); if (doujin == null) { return(false); } var context = Context as IDiscordContext; if (isFeed || Interactive?.Source?.Id != Context.User.Id) { context = new DiscordContextWrapper(context) { Channel = await Context.User.GetOrCreateDMChannelAsync() } } ; // send download interactive await _interactive.SendInteractiveAsync(new DownloadMessage(doujin), context, cancellationToken); return(true); } }
public async Task GetAsync(string source, string id, CancellationToken cancellationToken = default) { var doujin = await _database.GetDoujinAsync(GalleryUtility.ExpandContraction(source), id, cancellationToken); if (doujin == null) { await _context.ReplyAsync("doujinNotFound"); return; } await _interactive.SendInteractiveAsync(new DoujinMessage(doujin), _context, cancellationToken); }
public async Task ViewAsync(string name, CancellationToken cancellationToken = default) { name = FixCollectionName(name); // check if collection exists first var collection = await _database.GetCollectionAsync(_context.User.Id, name, cancellationToken); if (collection == null) { await _context.ReplyAsync("collectionNotFound", new { name }); return; } await _interactive.SendInteractiveAsync( new CollectionMessage(_context.User.Id, name), _context, cancellationToken); }
public Task LanguageAsync(CancellationToken cancellationToken = default) => _interactive.SendInteractiveAsync( new CommandHelpMessage { Command = "language", Aliases = new[] { "l" }, DescriptionKey = "options.language", Examples = new[] { "en", "english" } }, _context, cancellationToken);
public Task ListAsync(CancellationToken cancellationToken = default) => _interactive.SendInteractiveAsync(new CollectionListMessage(_context.User.Id), _context, cancellationToken);
public async Task <bool> TryHandleAsync(IMessageContext context, CancellationToken cancellationToken = default) { switch (context.Event) { case MessageEvent.Create: break; default: return(false); } var content = context.Message.Content; // ignore urls in commands if (content.StartsWith(_settings.Discord.Prefix)) { return(false); } // match gallery urls var ids = GalleryUtility.ParseMany(content); if (ids.Length == 0) { return(false); } if (_logger.IsEnabled(LogLevel.Debug)) { _logger.LogDebug($"Matched galleries: {string.Join(", ", ids.Select((s, i) => $"{s}/{i}"))}"); } // send interactive using (context.BeginTyping()) { // send one interactive if only one id detected if (ids.Length == 1) { var(source, id) = ids[0]; Doujin doujin; using (var scope = _services.CreateScope()) { doujin = await scope.ServiceProvider .GetRequiredService <IDatabase>() .GetDoujinAsync(source, id, cancellationToken); } if (doujin == null) { await context.ReplyAsync("doujinNotFound"); } else { await _interactive.SendInteractiveAsync( new DoujinMessage(doujin), context, cancellationToken); } } // send as a list else { await _interactive.SendInteractiveAsync( new GalleryUrlDetectedMessage(ids), context, cancellationToken); } } return(true); }