#httpRequest does not want to work and returns Error: System.Net.Http.HttpRequestException: Response

1 messages · Page 1 of 1 (latest)

left scroll
#

I'm trying to have the bot have some dictionary features and so I'm using FreeDictionaryAPI to do this.

public class DictionaryCommand : BaseCommandModule
{
    [Command("dict")]
    public async Task dictCommand(CommandContext ctx)
    {
        //await ctx.Channel.SendMessageAsync("Penis");

        var dictionary = new DictionaryOutput();

        Console.WriteLine(ctx.Message.Content.Remove(0, 5));
        Console.WriteLine(ctx.Message.Content);
        await dictionary.getDefinition(ctx.Message.Content.Remove(0, 5));

        Console.WriteLine(dictionary.defo);
        await ctx.Channel.SendMessageAsync(dictionary.defo); 
    }
}

should in theory run

public string defo { get; set; }

public async Task getDefinition(string message)
{
    //Dictionary stuff
    string DictionaryURL = "https://api.dictionaryapi.dev/api/v2/entries/en/" + message;
    Console.WriteLine(DictionaryURL);
    using (var client = new HttpClient())
    {
        try
        {
            var response = await client.GetStringAsync(DictionaryURL);
            List<Word>? word = JsonSerializer.Deserialize<List<Word>>(response.ToString());

            defo = word[0].meanings[0].definitions[0].definition;
            Console.WriteLine(defo);
        }
        catch (HttpRequestException exception)
        {
            Console.WriteLine("Error: {0}", exception);
        }
    }
}

But this does not happen. Also, if I write the command with a space (.dict hello), the entire program just ceases to work.

eternal kernel
#

you're declaring a parameterless command

#

(also, Task<string> exists to to return a value from an awaitable method)

#

as for whatever your HttpRequest is doing, that's not for this forum to figure out; but while i'm looking at this code you should create one HttpClient and reuse it - creating a new one every time is very problematic