示例#1
0
    public Task GroupCommand(CommandContext commandContext, [RemainingText] string searchTerm)
    {
        return(InvokeAsync(commandContext,
                           async commandContextContainer =>
        {
            if (string.IsNullOrWhiteSpace(searchTerm) == false)
            {
                await commandContext.Message
                .DeleteAsync()
                .ConfigureAwait(false);

                var rnd = new Random(DateTime.Now.Millisecond);

                var client = HttpClientFactory.CreateClient();

                using (var request = await client.GetAsync(QueryHelpers.AddQueryString("https://g.tenor.com/v1/search",
                                                                                       new Dictionary <string, string>
                {
                    ["q"] = searchTerm,
                    ["key"] = "RXM3VE2UGRU9",
                    ["limit"] = "10",
                    ["contentfilter"] = "high",
                    ["ar_range"] = "all",
                    ["media_filter"] = "minimal"
                }))
                                     .ConfigureAwait(false))
                {
                    var jsonResult = await request.Content
                                     .ReadAsStringAsync()
                                     .ConfigureAwait(false);

                    var searchResult = JsonConvert.DeserializeObject <SearchResultRoot>(jsonResult);

                    await commandContext.Channel
                    .SendMessageAsync(searchResult.Results[rnd.Next(searchResult.Results.Count - 1)]
                                      .ItemUrl)
                    .ConfigureAwait(false);

                    LoggingService.AddCommandLogEntry(LogEntryLevel.Information, commandContext.Command.QualifiedName, searchTerm, commandContext.User.ToString());
                }
            }
            else
            {
                await commandContextContainer.ShowHelp("gif")
                .ConfigureAwait(false);
            }
        }));
    }
示例#2
0
    /// <summary>
    /// Execution
    /// </summary>
    /// <param name="commandContext">Original command context</param>
    /// <param name="action">Action</param>
    /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
    protected async Task InvokeAsync(CommandContext commandContext, Func <CommandContextContainer, Task> action)
    {
        var commandContextContainer = new CommandContextContainer(commandContext, UserManagementService);

        try
        {
            await action(commandContextContainer).ConfigureAwait(false);
        }
        catch (TimeoutException)
        {
        }
        catch (OperationCanceledException)
        {
        }
        catch (ScruffyException ex)
        {
            await commandContextContainer.Channel
            .SendMessageAsync(ex.GetLocalizedMessage())
            .ConfigureAwait(false);
        }
        catch (Exception ex)
        {
            var logEntryId = LoggingService.AddCommandLogEntry(LogEntryLevel.CriticalError, commandContext.Command?.QualifiedName, commandContextContainer.LastUserMessage?.Content, ex.Message, ex.ToString());

            var client = HttpClientFactory.CreateClient();

            using (var response = await client.GetAsync("https://g.tenor.com/v1/search?q=funny%20cat&key=RXM3VE2UGRU9&limit=50&contentfilter=high&ar_range=all")
                                  .ConfigureAwait(false))
            {
                var jsonResult = await response.Content
                                 .ReadAsStringAsync()
                                 .ConfigureAwait(false);

                var searchResult = JsonConvert.DeserializeObject <SearchResultRoot>(jsonResult);

                var tenorEntry = searchResult.Results[new Random(DateTime.Now.Millisecond).Next(0, searchResult.Results.Count - 1)];

                var gifUrl = tenorEntry.Media[0].Gif.Size < 8_388_608
                                 ? tenorEntry.Media[0].Gif.Url
                                 : tenorEntry.Media[0].MediumGif.Size < 8_388_608
                                     ? tenorEntry.Media[0].MediumGif.Url
                                     : tenorEntry.Media[0].NanoGif.Url;

                using (var downloadResponse = await client.GetAsync(gifUrl)
                                              .ConfigureAwait(false))
                {
                    var stream = await downloadResponse.Content
                                 .ReadAsStreamAsync()
                                 .ConfigureAwait(false);

                    await using (stream.ConfigureAwait(false))
                    {
                        var internalLocalizationGroup = LocalizationService.GetGroup(nameof(LocatedCommandModuleBase));

                        var builder = new DiscordMessageBuilder().WithContent(internalLocalizationGroup.GetFormattedText("CommandFailedMessage", "The command could not be executed. But I have an error code ({0}) and funny cat picture.", logEntryId ?? -1))
                                      .WithFile("cat.gif", stream);

                        await commandContextContainer.Channel
                        .SendMessageAsync(builder)
                        .ConfigureAwait(false);
                    }
                }
            }
        }
    }
示例#3
0
    /// <summary>
    /// Called before a command in the implementing module is executed.
    /// </summary>
    /// <param name="commandContext">Context in which the method is being executed.</param>
    /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
    public override Task BeforeExecutionAsync(CommandContext commandContext)
    {
        LoggingService.AddCommandLogEntry(LogEntryLevel.Information, commandContext.Command?.QualifiedName, null, "BeforeExecution");

        return(Task.CompletedTask);
    }