From 3e1b5d7e572a064c2ea4712de868eb9f172fa40c Mon Sep 17 00:00:00 2001 From: Kwoth Date: Mon, 3 Apr 2017 23:18:18 +0200 Subject: [PATCH] some small fixes, .time added, closes #989 --- .../Modules/Administration/Administration.cs | 3 +- .../Commands/AutoAssignRoleCommands.cs | 6 +++ .../Commands/SelfAssignedRolesCommand.cs | 8 ++++ .../Searches/Commands/Models/TimeModels.cs | 42 +++++++++++++++++++ src/NadekoBot/Modules/Searches/Searches.cs | 21 ++++++++++ .../Resources/CommandStrings.Designer.cs | 29 ++++++++++++- src/NadekoBot/Resources/CommandStrings.resx | 13 +++++- .../Resources/ResponseStrings.Designer.cs | 20 ++++----- src/NadekoBot/Resources/ResponseStrings.resx | 9 ++-- src/NadekoBot/_Extensions/Extensions.cs | 2 +- 10 files changed, 134 insertions(+), 19 deletions(-) create mode 100644 src/NadekoBot/Modules/Searches/Commands/Models/TimeModels.cs diff --git a/src/NadekoBot/Modules/Administration/Administration.cs b/src/NadekoBot/Modules/Administration/Administration.cs index 4eec4ebd..6ecefe10 100644 --- a/src/NadekoBot/Modules/Administration/Administration.cs +++ b/src/NadekoBot/Modules/Administration/Administration.cs @@ -169,7 +169,8 @@ namespace NadekoBot.Modules.Administration var guser = (IGuildUser)Context.User; var userRoles = user.GetRoles(); - if (guser.Id != Context.Guild.OwnerId && (user.Id == Context.Guild.OwnerId || guser.GetRoles().Max(x => x.Position) <= userRoles.Max(x => x.Position))) + if (guser.Id != Context.Guild.OwnerId && + (user.Id == Context.Guild.OwnerId || guser.GetRoles().Max(x => x.Position) <= userRoles.Max(x => x.Position))) return; try { diff --git a/src/NadekoBot/Modules/Administration/Commands/AutoAssignRoleCommands.cs b/src/NadekoBot/Modules/Administration/Commands/AutoAssignRoleCommands.cs index 98a8844f..13a3422a 100644 --- a/src/NadekoBot/Modules/Administration/Commands/AutoAssignRoleCommands.cs +++ b/src/NadekoBot/Modules/Administration/Commands/AutoAssignRoleCommands.cs @@ -1,6 +1,7 @@ using Discord; using Discord.Commands; using NadekoBot.Attributes; +using NadekoBot.Extensions; using NadekoBot.Services; using NLog; using System; @@ -48,6 +49,11 @@ namespace NadekoBot.Modules.Administration [RequireUserPermission(GuildPermission.ManageRoles)] public async Task AutoAssignRole([Remainder] IRole role = null) { + var guser = (IGuildUser)Context.User; + if (role != null) + if (Context.User.Id != guser.Guild.OwnerId && guser.GetRoles().Max(x => x.Position) <= role.Position) + return; + using (var uow = DbHandler.UnitOfWork()) { var conf = uow.GuildConfigs.For(Context.Guild.Id, set => set); diff --git a/src/NadekoBot/Modules/Administration/Commands/SelfAssignedRolesCommand.cs b/src/NadekoBot/Modules/Administration/Commands/SelfAssignedRolesCommand.cs index 6ac60f21..e13a278f 100644 --- a/src/NadekoBot/Modules/Administration/Commands/SelfAssignedRolesCommand.cs +++ b/src/NadekoBot/Modules/Administration/Commands/SelfAssignedRolesCommand.cs @@ -43,6 +43,10 @@ namespace NadekoBot.Modules.Administration { IEnumerable roles; + var guser = (IGuildUser)Context.User; + if (Context.User.Id != guser.Guild.OwnerId && guser.GetRoles().Max(x => x.Position) <= role.Position) + return; + string msg; var error = false; using (var uow = DbHandler.UnitOfWork()) @@ -75,6 +79,10 @@ namespace NadekoBot.Modules.Administration [RequireUserPermission(GuildPermission.ManageRoles)] public async Task Rsar([Remainder] IRole role) { + var guser = (IGuildUser)Context.User; + if (Context.User.Id != guser.Guild.OwnerId && guser.GetRoles().Max(x => x.Position) <= role.Position) + return; + bool success; using (var uow = DbHandler.UnitOfWork()) { diff --git a/src/NadekoBot/Modules/Searches/Commands/Models/TimeModels.cs b/src/NadekoBot/Modules/Searches/Commands/Models/TimeModels.cs new file mode 100644 index 00000000..e997b78c --- /dev/null +++ b/src/NadekoBot/Modules/Searches/Commands/Models/TimeModels.cs @@ -0,0 +1,42 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NadekoBot.Modules.Searches.Commands.Models +{ + public class TimeZoneResult + { + public double DstOffset { get; set; } + public double RawOffset { get; set; } + + //public string TimeZoneId { get; set; } + public string TimeZoneName { get; set; } + } + + public class GeolocationResult + { + + public class GeolocationModel + { + public class GeometryModel + { + public class LocationModel + { + public float Lat { get; set; } + public float Lng { get; set; } + } + + public LocationModel Location { get; set; } + } + + [JsonProperty("formatted_address")] + public string FormattedAddress { get; set; } + public GeometryModel Geometry { get; set; } + } + + public GeolocationModel[] results; + } +} diff --git a/src/NadekoBot/Modules/Searches/Searches.cs b/src/NadekoBot/Modules/Searches/Searches.cs index 472a17d5..3e6aa1bc 100644 --- a/src/NadekoBot/Modules/Searches/Searches.cs +++ b/src/NadekoBot/Modules/Searches/Searches.cs @@ -56,6 +56,27 @@ namespace NadekoBot.Modules.Searches await Context.Channel.EmbedAsync(embed).ConfigureAwait(false); } + [NadekoCommand, Usage, Description, Aliases] + public async Task Time([Remainder] string arg) + { + if (string.IsNullOrWhiteSpace(arg) || string.IsNullOrWhiteSpace(NadekoBot.Credentials.GoogleApiKey)) + return; + + using (var http = new HttpClient()) + { + var res = await http.GetStringAsync($"https://maps.googleapis.com/maps/api/geocode/json?address={arg}&key={NadekoBot.Credentials.GoogleApiKey}").ConfigureAwait(false); + var obj = JsonConvert.DeserializeObject(res); + + var currentSeconds = DateTime.UtcNow.UnixTimestamp(); + var timeRes = await http.GetStringAsync($"https://maps.googleapis.com/maps/api/timezone/json?location={obj.results[0].Geometry.Location.Lat},{obj.results[0].Geometry.Location.Lng}×tamp={currentSeconds}&key={NadekoBot.Credentials.GoogleApiKey}").ConfigureAwait(false); + var timeObj = JsonConvert.DeserializeObject(timeRes); + + var time = DateTime.UtcNow.AddSeconds(timeObj.DstOffset + timeObj.RawOffset); + + await ReplyConfirmLocalized("time", Format.Bold(obj.results[0].FormattedAddress), Format.Code(time.ToString("HH:mm")), timeObj.TimeZoneName).ConfigureAwait(false); + } + } + [NadekoCommand, Usage, Description, Aliases] public async Task Youtube([Remainder] string query = null) { diff --git a/src/NadekoBot/Resources/CommandStrings.Designer.cs b/src/NadekoBot/Resources/CommandStrings.Designer.cs index c3e4c03b..c8f3ed92 100644 --- a/src/NadekoBot/Resources/CommandStrings.Designer.cs +++ b/src/NadekoBot/Resources/CommandStrings.Designer.cs @@ -5757,7 +5757,7 @@ namespace NadekoBot.Resources { } /// - /// Looks up a localized string similar quoteid qid. + /// Looks up a localized string similar to quoteid qid. /// public static string quoteid_cmd { get { @@ -8267,6 +8267,33 @@ namespace NadekoBot.Resources { } } + /// + /// Looks up a localized string similar to time. + /// + public static string time_cmd { + get { + return ResourceManager.GetString("time_cmd", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Shows the current time and timezone in the specified location.. + /// + public static string time_desc { + get { + return ResourceManager.GetString("time_desc", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to `{0}time London, UK`. + /// + public static string time_usage { + get { + return ResourceManager.GetString("time_usage", resourceCulture); + } + } + /// /// Looks up a localized string similar to timezone. /// diff --git a/src/NadekoBot/Resources/CommandStrings.resx b/src/NadekoBot/Resources/CommandStrings.resx index ec1f2125..1d5479ab 100644 --- a/src/NadekoBot/Resources/CommandStrings.resx +++ b/src/NadekoBot/Resources/CommandStrings.resx @@ -1152,7 +1152,7 @@ `{0}qsearch keyword text` - + quoteid qid @@ -3357,4 +3357,13 @@ `{0}slowmodewl SomeRole` or `{0}slowmodewl AdminDude` - + + time + + + Shows the current time and timezone in the specified location. + + + `{0}time London, UK` + + \ No newline at end of file diff --git a/src/NadekoBot/Resources/ResponseStrings.Designer.cs b/src/NadekoBot/Resources/ResponseStrings.Designer.cs index 9561c996..f4171166 100644 --- a/src/NadekoBot/Resources/ResponseStrings.Designer.cs +++ b/src/NadekoBot/Resources/ResponseStrings.Designer.cs @@ -5773,6 +5773,15 @@ namespace NadekoBot.Resources { } } + /// + /// Looks up a localized string similar to Time in {0} is {1} - {2}. + /// + public static string searches_time { + get { + return ResourceManager.GetString("searches_time", resourceCulture); + } + } + /// /// Looks up a localized string similar to Title:. /// @@ -6498,7 +6507,7 @@ namespace NadekoBot.Resources { } /// - /// Looks up a localized string similar to No quotes on this page.. + /// Looks up a localized string similar to No quotes found matching the quote ID specified.. /// public static string utility_quotes_page_none { get { @@ -6506,15 +6515,6 @@ namespace NadekoBot.Resources { } } - /// - /// Looks up a localized string similar to No quotes found matching the quote ID specified.. - /// - public static string utility_quotes_notfound { - get { - return ResourceManager.GetString("utility_quotes_notfound", resourceCulture); - } - } - /// /// Looks up a localized string similar to No quotes found which you can remove.. /// diff --git a/src/NadekoBot/Resources/ResponseStrings.resx b/src/NadekoBot/Resources/ResponseStrings.resx index 31b11e93..2a1d1e7f 100644 --- a/src/NadekoBot/Resources/ResponseStrings.resx +++ b/src/NadekoBot/Resources/ResponseStrings.resx @@ -2082,10 +2082,7 @@ Owner ID: {2} Page {0} of quotes - No quotes on this page. - No quotes found matching the quote ID specified. - No quotes found which you can remove. @@ -2405,4 +2402,8 @@ Owner ID: {2} Rewards can be claimed on or after 5th of each month. - + + Time in {0} is {1} - {2} + Time in London, UK is 15:30 - Time Zone Name + + \ No newline at end of file diff --git a/src/NadekoBot/_Extensions/Extensions.cs b/src/NadekoBot/_Extensions/Extensions.cs index b880944c..52d8ab3a 100644 --- a/src/NadekoBot/_Extensions/Extensions.cs +++ b/src/NadekoBot/_Extensions/Extensions.cs @@ -193,7 +193,7 @@ namespace NadekoBot.Extensions public static string SanitizeMentions(this string str) => str.Replace("@everyone", "@everyοne").Replace("@here", "@һere"); - public static double UnixTimestamp(this DateTime dt) => dt.ToUniversalTime().Subtract(new DateTime(1970, 1, 1)).TotalSeconds; + public static double UnixTimestamp(this DateTime dt) => dt.ToUniversalTime().Subtract(new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds; public static async Task SendMessageAsync(this IUser user, string message, bool isTTS = false) => await (await user.CreateDMChannelAsync().ConfigureAwait(false)).SendMessageAsync(message, isTTS).ConfigureAwait(false);