You can now use %server_time% placeholder to get your server's time, if you've set your timezone

This commit is contained in:
Master Kwoth 2017-07-05 13:17:59 +02:00
parent 65b76ec48e
commit d089a37bf0
4 changed files with 26 additions and 4 deletions

View File

@ -18,6 +18,7 @@ Some features have their own specific placeholders which are noted in that featu
- `%userdiscrim%` - discriminator (for example 1234) - `%userdiscrim%` - discriminator (for example 1234)
- `%rngX-Y%` - Replace X and Y with the range (for example `%rng5-10%` - random between 5 and 10) - `%rngX-Y%` - Replace X and Y with the range (for example `%rng5-10%` - random between 5 and 10)
- `%time%` - Bot time - `%time%` - Bot time
- `%server_time%` - Time on this server, set with `.timezone` command
**If you're using placeholders in embeds, don't use %user% and in titles, footers and field names. They will not show properly.** **If you're using placeholders in embeds, don't use %user% and in titles, footers and field names. They will not show properly.**

View File

@ -3,6 +3,7 @@ using Discord.Commands;
using Discord.WebSocket; using Discord.WebSocket;
using NadekoBot.Extensions; using NadekoBot.Extensions;
using NadekoBot.Services; using NadekoBot.Services;
using NadekoBot.Services.Administration;
using NadekoBot.Services.Music; using NadekoBot.Services.Music;
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
@ -26,7 +27,7 @@ namespace NadekoBot.DataStructures.Replacements
{ {
return this.WithUser(usr) return this.WithUser(usr)
.WithChannel(ch) .WithChannel(ch)
.WithServer(g) .WithServer(client, g)
.WithClient(client); .WithClient(client);
} }
@ -41,10 +42,24 @@ namespace NadekoBot.DataStructures.Replacements
return this; return this;
} }
public ReplacementBuilder WithServer(IGuild g) public ReplacementBuilder WithServer(DiscordSocketClient client, IGuild g)
{ {
_reps.TryAdd("%sid%", () => g == null ? "DM" : g.Id.ToString()); _reps.TryAdd("%sid%", () => g == null ? "DM" : g.Id.ToString());
_reps.TryAdd("%server%", () => g == null ? "DM" : g.Name); _reps.TryAdd("%server%", () => g == null ? "DM" : g.Name);
_reps.TryAdd("%server_time%", () =>
{
TimeZoneInfo to = TimeZoneInfo.Local;
if (g != null)
{
if (GuildTimezoneService.AllServices.TryGetValue(client.CurrentUser.Id, out var tz))
to = tz.GetTimeZoneOrDefault(g.Id) ?? TimeZoneInfo.Local;
}
return TimeZoneInfo.ConvertTime(DateTime.UtcNow,
TimeZoneInfo.Utc,
to).ToString("HH:mm ") + to.StandardName.GetInitials();
});
return this; return this;
} }

View File

@ -205,7 +205,7 @@ namespace NadekoBot
var gameVcService = new GameVoiceChannelService(Client, Db, AllGuildConfigs); var gameVcService = new GameVoiceChannelService(Client, Db, AllGuildConfigs);
var autoAssignRoleService = new AutoAssignRoleService(Client, AllGuildConfigs); var autoAssignRoleService = new AutoAssignRoleService(Client, AllGuildConfigs);
var logCommandService = new LogCommandService(Client, Strings, AllGuildConfigs, Db, muteService, protectionService); var logCommandService = new LogCommandService(Client, Strings, AllGuildConfigs, Db, muteService, protectionService);
var guildTimezoneService = new GuildTimezoneService(AllGuildConfigs, Db); var guildTimezoneService = new GuildTimezoneService(Client, AllGuildConfigs, Db);
#endregion #endregion
#region pokemon #region pokemon

View File

@ -5,15 +5,18 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using NadekoBot.Services; using NadekoBot.Services;
using Discord.WebSocket;
namespace NadekoBot.Services.Administration namespace NadekoBot.Services.Administration
{ {
public class GuildTimezoneService public class GuildTimezoneService
{ {
//hack >.>
public static ConcurrentDictionary<ulong, GuildTimezoneService> AllServices { get; } = new ConcurrentDictionary<ulong, GuildTimezoneService>();
private ConcurrentDictionary<ulong, TimeZoneInfo> _timezones; private ConcurrentDictionary<ulong, TimeZoneInfo> _timezones;
private readonly DbService _db; private readonly DbService _db;
public GuildTimezoneService(IEnumerable<GuildConfig> gcs, DbService db) public GuildTimezoneService(DiscordSocketClient client, IEnumerable<GuildConfig> gcs, DbService db)
{ {
_timezones = gcs _timezones = gcs
.Select(x => .Select(x =>
@ -36,6 +39,9 @@ namespace NadekoBot.Services.Administration
.ToDictionary(x => x.Item1, x => x.Item2) .ToDictionary(x => x.Item1, x => x.Item2)
.ToConcurrent(); .ToConcurrent();
var curUser = client.CurrentUser;
if (curUser != null)
AllServices.TryAdd(curUser.Id, this);
_db = db; _db = db;
} }