-
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 #105 from Zechiax/v3base
V3 Upgrade
- Loading branch information
Showing
28 changed files
with
1,005 additions
and
438 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,53 @@ | ||
using Asterion.Extensions; | ||
|
||
namespace Asterion.Test; | ||
|
||
[TestFixture] | ||
public class SplitExtensionTests | ||
{ | ||
[Test] | ||
public void TestSliceOnEmptyArray() | ||
{ | ||
var array = Array.Empty<int>(); | ||
|
||
var result = array.Split(10).ToArray(); | ||
|
||
Assert.That(result, Is.Empty); | ||
} | ||
|
||
[Test] | ||
public void TestSplitEvenArray() | ||
{ | ||
int[] numbers = {1, 2, 3, 4, 5, 6}; | ||
var segments = numbers.Split(2).ToArray(); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(segments, Has.Length.EqualTo(3)); | ||
Assert.That(segments[0], Has.Count.EqualTo(2)); | ||
Assert.That(segments[0].Array, Is.Not.Null); | ||
Assert.That(segments[0].Array![segments[0].Offset], Is.EqualTo(1)); | ||
Assert.That(segments[0].Array![segments[0].Offset + 1], Is.EqualTo(2)); | ||
}); | ||
} | ||
|
||
[Test] | ||
public void TestSplitOddArray() | ||
{ | ||
int[] numbers = {1, 2, 3, 4, 5, 6, 7}; | ||
var segments = numbers.Split(3).ToArray(); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(segments, Has.Length.EqualTo(3)); | ||
Assert.That(segments[0], Has.Count.EqualTo(3)); | ||
Assert.That(segments[0].Array, Is.Not.Null); | ||
Assert.That(segments[0].Array![segments[0].Offset], Is.EqualTo(1)); | ||
Assert.That(segments[0].Array![segments[0].Offset + 1], Is.EqualTo(2)); | ||
Assert.That(segments[0].Array![segments[0].Offset + 2], Is.EqualTo(3)); | ||
|
||
// Last segment should have 1 element | ||
Assert.That(segments[2], Has.Count.EqualTo(1)); | ||
}); | ||
} | ||
} |
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 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,58 @@ | ||
using System.Globalization; | ||
using Asterion.EmbedBuilders; | ||
using Asterion.Interfaces; | ||
using Discord; | ||
using Discord.Interactions; | ||
using Color = Discord.Color; | ||
|
||
namespace Asterion.Common; | ||
|
||
public class AsterionInteractionModuleBase : InteractionModuleBase<SocketInteractionContext> | ||
{ | ||
protected CultureInfo? CommandCultureInfo { get; set; } | ||
protected ILocalizationService LocalizationService { get; } | ||
public AsterionInteractionModuleBase(ILocalizationService localizationService) | ||
{ | ||
LocalizationService = localizationService; | ||
} | ||
|
||
protected async Task FollowupWithSearchResultErrorAsync<T>(Services.Modrinth.SearchResult<T> status) | ||
{ | ||
if (status.Success) | ||
{ | ||
throw new ArgumentException("SearchResult was successful, but was expected to be an error"); | ||
} | ||
|
||
string title = LocalizationService.Get("Modrinth_Search_Unsuccessful", CommandCultureInfo); | ||
|
||
string? description; | ||
switch (status.SearchStatus) | ||
{ | ||
case Services.Modrinth.SearchStatus.ApiDown: | ||
description = LocalizationService.Get("Error_ModrinthApiUnavailable", CommandCultureInfo); | ||
title += ". " + LocalizationService.Get("Error_TryAgainLater", CommandCultureInfo); | ||
break; | ||
case Services.Modrinth.SearchStatus.NoResult: | ||
description = LocalizationService.Get("Modrinth_Search_NoResult_WithQuery", CommandCultureInfo, new object[] {status.Query}); | ||
break; | ||
case Services.Modrinth.SearchStatus.UnknownError: | ||
description = LocalizationService.Get("Error_Unknown", CommandCultureInfo); | ||
title += ". " + LocalizationService.Get("Error_TryAgainLater", CommandCultureInfo); | ||
break; | ||
default: | ||
throw new ArgumentOutOfRangeException(); | ||
} | ||
|
||
var embed = CommonEmbedBuilder.GetErrorEmbedBuilder(title, description).Build(); | ||
|
||
await FollowupAsync(embeds: new[] {embed}); | ||
} | ||
|
||
public override void BeforeExecute(ICommandInfo cmd) | ||
{ | ||
// We currently set US culture for all commands | ||
CommandCultureInfo = CultureInfo.GetCultureInfo("en-US"); | ||
|
||
base.BeforeExecute(cmd); | ||
} | ||
} |
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,48 @@ | ||
using Discord; | ||
using Color = Discord.Color; | ||
|
||
namespace Asterion.EmbedBuilders; | ||
|
||
public static class CommonEmbedBuilder | ||
{ | ||
private static Color _errorColor = Color.Red; | ||
private static Color _warningColor = Color.Orange; | ||
private static Color _infoColor = Color.Blue; | ||
private static Color _successColor = Color.Green; | ||
|
||
public static EmbedBuilder GetSuccessEmbedBuilder(string title, string description) | ||
{ | ||
return new EmbedBuilder() | ||
.WithTitle(title) | ||
.WithDescription(description) | ||
.WithColor(_successColor) | ||
.WithCurrentTimestamp(); | ||
} | ||
|
||
public static EmbedBuilder GetErrorEmbedBuilder(string title, string description) | ||
{ | ||
return new EmbedBuilder() | ||
.WithTitle(title) | ||
.WithDescription(description) | ||
.WithColor(_errorColor) | ||
.WithCurrentTimestamp(); | ||
} | ||
|
||
public static EmbedBuilder GetInfoEmbedBuilder(string title, string description) | ||
{ | ||
return new EmbedBuilder() | ||
.WithTitle(title) | ||
.WithDescription(description) | ||
.WithColor(_infoColor) | ||
.WithCurrentTimestamp(); | ||
} | ||
|
||
public static EmbedBuilder GetWarningEmbedBuilder(string title, string description) | ||
{ | ||
return new EmbedBuilder() | ||
.WithTitle(title) | ||
.WithDescription(description) | ||
.WithColor(_warningColor) | ||
.WithCurrentTimestamp(); | ||
} | ||
} |
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,16 @@ | ||
namespace Asterion.Extensions; | ||
|
||
public static class ArrayExtensions | ||
{ | ||
public static IEnumerable<ArraySegment<T>> Split<T>(this T[] array, int blockSize) | ||
{ | ||
var offset = 0; | ||
while (offset < array.Length) | ||
{ | ||
var remaining = array.Length - offset; | ||
var blockSizeToUse = Math.Min(remaining, blockSize); | ||
yield return new ArraySegment<T>(array, offset, blockSizeToUse); | ||
offset += blockSizeToUse; | ||
} | ||
} | ||
} |
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,13 @@ | ||
using System.Globalization; | ||
using Discord.Interactions; | ||
using Microsoft.Extensions.Localization; | ||
|
||
namespace Asterion.Interfaces; | ||
|
||
public interface ILocalizationService | ||
{ | ||
LocalizedString Get(string key); | ||
LocalizedString Get(string key, CultureInfo? cultureInfo); | ||
LocalizedString Get(string key, params object[] parameters); | ||
LocalizedString Get(string key, CultureInfo? cultureInfo, params object[] parameters); | ||
} |
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 |
---|---|---|
@@ -1,13 +1,20 @@ | ||
using Discord.Interactions; | ||
using Asterion.Common; | ||
using Asterion.Interfaces; | ||
using Discord.Interactions; | ||
using Discord.WebSocket; | ||
|
||
namespace Asterion.Modules; | ||
|
||
public class BotCommands : InteractionModuleBase<SocketInteractionContext> | ||
public class BotCommands : AsterionInteractionModuleBase | ||
{ | ||
#if DEBUG | ||
[SlashCommand("ping", "Pings the bot", runMode: RunMode.Async)] | ||
public async Task Ping() | ||
{ | ||
await RespondAsync($"Pong :ping_pong: It took me {Context.Client.Latency}ms to respond to you", | ||
ephemeral: true); | ||
await RespondAsync($"Pong! Latency: {Context.Client.Latency}ms"); | ||
} | ||
#endif | ||
public BotCommands(ILocalizationService localizationService) : base(localizationService) | ||
{ | ||
} | ||
} |
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
Oops, something went wrong.