1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-01-31 09:09:00 +03:00

Add /timestamp (#140)

Original idea from @Octol1ttle

---------

Signed-off-by: Macintosh II <mctaylxrs@outlook.com>
This commit is contained in:
Macintxsh 2023-10-03 15:07:41 +03:00 committed by GitHub
parent 74a287ba72
commit 413b8a4781
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 105 additions and 1 deletions

View file

@ -654,4 +654,10 @@
<data name="RandomOutput" xml:space="preserve">
<value>Your random number is:</value>
</data>
<data name="TimestampTitle" xml:space="preserve">
<value>Timestamp for {0}:</value>
</data>
<data name="TimestampOffset" xml:space="preserve">
<value>Offset: {0}</value>
</data>
</root>

View file

@ -654,4 +654,10 @@
<data name="RandomOutput" xml:space="preserve">
<value>Ваше случайное число:</value>
</data>
<data name="TimestampTitle" xml:space="preserve">
<value>Временная метка для {0}:</value>
</data>
<data name="TimestampOffset" xml:space="preserve">
<value>Офсет: {0}</value>
</data>
</root>

View file

@ -654,4 +654,10 @@
<data name="RandomOutput" xml:space="preserve">
<value>ваше рандомное число:</value>
</data>
<data name="TimestampTitle" xml:space="preserve">
<value>таймштамп для {0}:</value>
</data>
<data name="TimestampOffset" xml:space="preserve">
<value>офсет: {0}</value>
</data>
</root>

View file

@ -19,7 +19,7 @@ using Remora.Results;
namespace Octobot.Commands;
/// <summary>
/// Handles tool commands: /showinfo, /random.
/// Handles tool commands: /showinfo, /random, /timestamp.
/// </summary>
[UsedImplicitly]
public class ToolsCommandGroup : CommandGroup
@ -289,4 +289,74 @@ public class ToolsCommandGroup : CommandGroup
return await _feedback.SendContextualEmbedResultAsync(embed, ct);
}
private static readonly TimestampStyle[] AllStyles =
{
TimestampStyle.ShortDate,
TimestampStyle.LongDate,
TimestampStyle.ShortTime,
TimestampStyle.LongTime,
TimestampStyle.ShortDateTime,
TimestampStyle.LongDateTime,
TimestampStyle.RelativeTime
};
/// <summary>
/// A slash command that shows the current timestamp with an optional offset in all styles supported by Discord.
/// </summary>
/// <param name="offset">The offset for the current timestamp.</param>
/// <returns>
/// A feedback sending result which may or may not have succeeded.
/// </returns>
[Command("timestamp")]
[DiscordDefaultDMPermission(false)]
[Description("Shows a timestamp in all styles")]
[UsedImplicitly]
public async Task<Result> ExecuteTimestampAsync(
[Description("Offset from current time")]
TimeSpan? offset = null)
{
if (!_context.TryGetContextIDs(out var guildId, out _, out var userId))
{
return new ArgumentInvalidError(nameof(_context), "Unable to retrieve necessary IDs from command context");
}
var userResult = await _userApi.GetUserAsync(userId, CancellationToken);
if (!userResult.IsDefined(out var user))
{
return Result.FromError(userResult);
}
var data = await _guildData.GetData(guildId, CancellationToken);
Messages.Culture = GuildSettings.Language.Get(data.Settings);
return await SendTimestampAsync(offset, user, CancellationToken);
}
private async Task<Result> SendTimestampAsync(TimeSpan? offset, IUser user, CancellationToken ct)
{
var timestamp = DateTimeOffset.UtcNow.Add(offset ?? TimeSpan.Zero).ToUnixTimeSeconds();
var description = new StringBuilder().Append("# ").AppendLine(timestamp.ToString());
if (offset is not null)
{
description.AppendLine(string.Format(
Messages.TimestampOffset, Markdown.InlineCode(offset.ToString() ?? string.Empty))).AppendLine();
}
foreach (var markdownTimestamp in AllStyles.Select(style => Markdown.Timestamp(timestamp, style)))
{
description.Append("- ").Append(Markdown.InlineCode(markdownTimestamp))
.Append(" → ").AppendLine(markdownTimestamp);
}
var embed = new EmbedBuilder().WithSmallTitle(
string.Format(Messages.TimestampTitle, user.GetTag()), user)
.WithDescription(description.ToString())
.WithColour(ColorsList.Blue)
.Build();
return await _feedback.SendContextualEmbedResultAsync(embed, ct);
}
}

View file

@ -1135,5 +1135,21 @@ namespace Octobot {
return ResourceManager.GetString("RandomOutput", resourceCulture);
}
}
internal static string TimestampTitle
{
get
{
return ResourceManager.GetString("TimestampTitle", resourceCulture);
}
}
internal static string TimestampOffset
{
get
{
return ResourceManager.GetString("TimestampOffset", resourceCulture);
}
}
}
}