1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-05-05 21:46:28 +03:00

Add /timestamp

Signed-off-by: Macintosh II <mctaylxrs@outlook.com>
This commit is contained in:
Macintxsh 2023-10-02 23:53:33 +03:00
parent d837745b11
commit 6ba2127b35
Signed by: mctaylors
GPG key ID: 361D326747B61E65
5 changed files with 95 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,64 @@ public class ToolsCommandGroup : CommandGroup
return await _feedback.SendContextualEmbedResultAsync(embed, ct);
}
/// <summary>
/// A slash command that shows current or counted timestamp.
/// </summary>
/// <param name="offset">The offset to count timestamp.</param>
/// <returns>
/// A feedback sending result which may or may not have succeeded.
/// </returns>
[Command("timestamp")]
[DiscordDefaultDMPermission(false)]
[Description("Gets timestamp")]
[UsedImplicitly]
public async Task<Result> ExecuteTimestampAsync(
[Description("Add an offset from now")]
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 SendRandomNumberAsync(offset, user, CancellationToken);
}
private async Task<Result> SendRandomNumberAsync(TimeSpan? offset, IUser user, CancellationToken ct)
{
var timestamp = DateTimeOffset.UtcNow.Add(offset ?? TimeSpan.Zero).ToUnixTimeSeconds().ToString();
var description = new StringBuilder().Append("# ").AppendLine(timestamp);
if (offset is not null)
{
description.AppendLine(string.Format(
Messages.TimestampOffset, Markdown.InlineCode(offset.ToString() ?? string.Empty))).AppendLine();
}
string[] options = { "d", "D", "t", "T", "f", "F", "R" };
foreach (var option in options)
{
description.Append("- ").Append(Markdown.InlineCode($"<t:{timestamp}:{option}>"))
.Append(" — ").AppendLine($"<t:{timestamp}:{option}>");
}
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);
}
}
}
}