Refaction all files into component based folders
This commit is contained in:
parent
55e152f4aa
commit
e3adf55742
102 changed files with 816 additions and 709 deletions
68
Geekbot.net/Commands/Integrations/Google/Google.cs
Normal file
68
Geekbot.net/Commands/Integrations/Google/Google.cs
Normal file
|
@ -0,0 +1,68 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Geekbot.net.Lib;
|
||||
using Geekbot.net.Lib.ErrorHandling;
|
||||
using StackExchange.Redis;
|
||||
using Utf8Json;
|
||||
|
||||
namespace Geekbot.net.Commands.Integrations.Google
|
||||
{
|
||||
public class Google : ModuleBase
|
||||
{
|
||||
private readonly IErrorHandler _errorHandler;
|
||||
private readonly IDatabase _redis;
|
||||
|
||||
public Google(IErrorHandler errorHandler, IDatabase redis)
|
||||
{
|
||||
_errorHandler = errorHandler;
|
||||
_redis = redis;
|
||||
}
|
||||
|
||||
[Command("google", RunMode = RunMode.Async)]
|
||||
[Remarks(CommandCategories.Helpers)]
|
||||
[Summary("Google Something.")]
|
||||
public async Task AskGoogle([Remainder, Summary("SearchText")] string searchText)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var client = new WebClient())
|
||||
{
|
||||
var apiKey = _redis.StringGet("googleGraphKey");
|
||||
if (!apiKey.HasValue)
|
||||
{
|
||||
await ReplyAsync("No Google API key has been set, please contact my owner");
|
||||
return;
|
||||
}
|
||||
|
||||
var url = new Uri($"https://kgsearch.googleapis.com/v1/entities:search?languages=en&limit=1&query={searchText}&key={apiKey}");
|
||||
var responseString = client.DownloadString(url);
|
||||
var response = JsonSerializer.Deserialize<GoogleKgApiResponseDto>(responseString);
|
||||
|
||||
if (!response.ItemListElement.Any())
|
||||
{
|
||||
await ReplyAsync("No results were found...");
|
||||
return;
|
||||
}
|
||||
|
||||
var data = response.ItemListElement.First().ResultDto;
|
||||
var eb = new EmbedBuilder();
|
||||
eb.Title = data.Name;
|
||||
if(!string.IsNullOrEmpty(data.Description)) eb.WithDescription(data.Description);
|
||||
if(!string.IsNullOrEmpty(data.DetailedDtoDescription?.Url)) eb.WithUrl(data.DetailedDtoDescription.Url);
|
||||
if(!string.IsNullOrEmpty(data.DetailedDtoDescription?.ArticleBody)) eb.AddField("Details", data.DetailedDtoDescription.ArticleBody);
|
||||
if(!string.IsNullOrEmpty(data.ImageDto?.ContentUrl)) eb.WithThumbnailUrl(data.ImageDto.ContentUrl);
|
||||
|
||||
await ReplyAsync("", false, eb.Build());
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_errorHandler.HandleCommandException(e, Context);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
namespace Geekbot.net.Commands.Integrations.Google
|
||||
{
|
||||
public class GoogleKgApiDetailedDto
|
||||
{
|
||||
public string ArticleBody { get; set; }
|
||||
public string Url { get; set; }
|
||||
public string License { get; set; }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
namespace Geekbot.net.Commands.Integrations.Google
|
||||
{
|
||||
public class GoogleKgApiElementDto
|
||||
{
|
||||
public GoogleKgApiResultDto ResultDto { get; set; }
|
||||
public double ResultScore { get; set; }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
namespace Geekbot.net.Commands.Integrations.Google
|
||||
{
|
||||
public class GoogleKgApiImageDto
|
||||
{
|
||||
public string ContentUrl { get; set; }
|
||||
public string Url { get; set; }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace Geekbot.net.Commands.Integrations.Google
|
||||
{
|
||||
public class GoogleKgApiResponseDto
|
||||
{
|
||||
public List<GoogleKgApiElementDto> ItemListElement { get; set; }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
namespace Geekbot.net.Commands.Integrations.Google
|
||||
{
|
||||
public class GoogleKgApiResultDto
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Description { get; set; }
|
||||
public GoogleKgApiImageDto ImageDto { get; set; }
|
||||
public GoogleKgApiDetailedDto DetailedDtoDescription { get; set; }
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue