-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from KevinDaGame/development
Development
- Loading branch information
Showing
8 changed files
with
243 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
using Discord; | ||
using Discord.WebSocket; | ||
using Flurl.Http; | ||
using Microsoft.Extensions.Logging; | ||
using RemindoBot.Services; | ||
|
||
namespace RemindoBot.Handlers; | ||
|
||
public class UrbanButtonHandler : IHandler | ||
{ | ||
private readonly DiscordSocketClient _client; | ||
private readonly ILogger<VoiceChatStatusHandler> _logger; | ||
private readonly IUrbanDictionaryService _urbanDictionaryService; | ||
private readonly IActualDictionaryService _actualDictionaryService; | ||
|
||
public UrbanButtonHandler(DiscordSocketClient client, ILogger<VoiceChatStatusHandler> logger, | ||
IUrbanDictionaryService urbanDictionaryService, | ||
IActualDictionaryService actualDictionaryService) | ||
{ | ||
_client = client; | ||
_logger = logger; | ||
_urbanDictionaryService = urbanDictionaryService; | ||
_actualDictionaryService = actualDictionaryService; | ||
} | ||
|
||
public void RegisterHandlers() | ||
{ | ||
_client.ButtonExecuted += HandleUrbanButton; | ||
} | ||
|
||
public async Task HandleUrbanButton(SocketMessageComponent component) | ||
{ | ||
if (component.Data.CustomId.StartsWith("urban-")) | ||
{ | ||
string word = component.Data.CustomId.Split("-")[1]; | ||
int number = int.Parse(component.Data.CustomId.Split("-")[^1]); | ||
|
||
if (number < 0) | ||
{ | ||
await component.RespondAsync("Invalid definition number"); | ||
return; | ||
} | ||
|
||
UrbanDictionaryDefinition definition; | ||
int definitionCount; | ||
try | ||
{ | ||
var definitions = await _urbanDictionaryService.GetDefinitionsOfWord(word); | ||
if (number >= definitions.Count) | ||
{ | ||
await component.RespondAsync("no more definitions"); | ||
return; | ||
} | ||
|
||
definitionCount = definitions.Count; | ||
definition = definitions[number]; | ||
} | ||
catch (FlurlHttpException) | ||
{ | ||
await component.RespondAsync("Failed to fetch definition"); | ||
return; | ||
} | ||
|
||
EmbedBuilder? embed = new EmbedBuilder() | ||
.WithTitle(definition.Word) | ||
.WithDescription(definition.Definition) | ||
.WithUrl(definition.Permalink) | ||
.WithFooter( | ||
$"{definition.Author} - [{number + 1}/{definitionCount}] - {definition.Thumbs_up} 👍 {definition.Thumbs_down} 👎") | ||
.WithColor(Color.Blue) | ||
.WithTimestamp(DateTime.Parse(definition.Written_on)); | ||
|
||
ComponentBuilder buttonsBuilder = new ComponentBuilder() | ||
.WithButton("View on Urban Dictionary", style: ButtonStyle.Link, url: definition.Permalink); | ||
|
||
if (number > 0) | ||
{ | ||
buttonsBuilder.WithButton("Previous definition", customId: $"urban-{word}-{number - 1}"); | ||
} | ||
|
||
if (number < definitionCount - 1) | ||
{ | ||
buttonsBuilder.WithButton("Next definition", customId: $"urban-{word}-{number + 1}"); | ||
} | ||
|
||
buttonsBuilder.WithButton("Actual definition", style: ButtonStyle.Success, customId: $"actual-{word}-0"); | ||
|
||
await component.Message.ModifyAsync(properties => | ||
{ | ||
properties.Embed = embed.Build(); | ||
properties.Components = buttonsBuilder.Build(); | ||
}); | ||
await component.DeferAsync(); | ||
} | ||
|
||
if (component.Data.CustomId.StartsWith("actual-")) | ||
{ | ||
string word = component.Data.CustomId.Split("-")[1]; | ||
int number = int.Parse(component.Data.CustomId.Split("-")[^1]); | ||
if (number < 0) | ||
{ | ||
await component.RespondAsync("Invalid definition number"); | ||
return; | ||
} | ||
|
||
ActualDictionaryDefinition definition; | ||
int definitionCount; | ||
try | ||
{ | ||
var definitions = await _actualDictionaryService.GetDefinitionsOfWord(word); | ||
if (number >= definitions.Count) | ||
{ | ||
await component.RespondAsync("no more definitions"); | ||
return; | ||
} | ||
|
||
definitionCount = definitions.Count; | ||
definition = definitions[number]; | ||
} | ||
catch (FlurlHttpException e) | ||
{ | ||
await component.RespondAsync("No definition found"); | ||
_logger.LogError(e, "Failed to fetch definition"); | ||
return; | ||
} | ||
|
||
EmbedBuilder? embed = new EmbedBuilder() | ||
.WithTitle(definition.Word) | ||
.WithDescription(definition.Definition) | ||
.WithFooter($"[{number + 1}/{definitionCount}]") | ||
.WithColor(Color.Blue); | ||
|
||
ComponentBuilder buttonsBuilder = new ComponentBuilder(); | ||
if (number > 0) | ||
{ | ||
buttonsBuilder.WithButton("Previous definition", customId: $"actual-{word}-{number - 1}"); | ||
} | ||
|
||
if (number < definitionCount - 1) | ||
{ | ||
buttonsBuilder.WithButton("Next definition", customId: $"actual-{word}-{number + 1}"); | ||
} | ||
|
||
await component.Message.ModifyAsync(properties => | ||
{ | ||
properties.Embed = embed.Build(); | ||
properties.Components = buttonsBuilder.Build(); | ||
}); | ||
await component.DeferAsync(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using Flurl; | ||
using Flurl.Http; | ||
|
||
namespace RemindoBot.Services; | ||
|
||
public class ActualDictionaryService : IActualDictionaryService | ||
{ | ||
string baseURI = "https://api.dictionaryapi.dev/api/v2/entries/en/"; | ||
|
||
public async Task<List<ActualDictionaryDefinition>> GetDefinitionsOfWord(string word) | ||
{ | ||
var result = await baseURI.AppendPathSegment(word).GetJsonAsync<ActualDictionaryResponse[]>(); | ||
return result.First().Meanings.Select(m => new ActualDictionaryDefinition | ||
{ | ||
Word = result.First().Word, | ||
Definition = m.Definitions.First().Definition | ||
}).ToList(); | ||
} | ||
|
||
public Task<ActualDictionaryDefinition?> GetDefinitionOfWord(string world, out int definitionCount) | ||
{ | ||
List<ActualDictionaryDefinition> result = GetDefinitionsOfWord(world).Result; | ||
definitionCount = result.Count; | ||
return Task.FromResult(result.Count == 0 ? null : result.FirstOrDefault()); | ||
} | ||
} | ||
|
||
public class ActualDictionaryResponse | ||
{ | ||
public string Word { get; set; } | ||
public List<ActualDictionaryMeaning> Meanings { get; set; } | ||
} | ||
|
||
public class ActualDictionaryMeaning | ||
{ | ||
public List<ActualDictionaryDefinitionR> Definitions { get; set; } | ||
} | ||
|
||
public class ActualDictionaryDefinitionR | ||
{ | ||
public string Definition { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace RemindoBot.Services; | ||
|
||
public interface IActualDictionaryService | ||
{ | ||
public Task<List<ActualDictionaryDefinition>> GetDefinitionsOfWord(string word); | ||
public Task<ActualDictionaryDefinition?> GetDefinitionOfWord(string world, out int definitionCount); | ||
} | ||
|
||
public class ActualDictionaryDefinition | ||
{ | ||
public string Word { get; set; } | ||
public string Definition { get; set; } | ||
public string Example { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.