Use DateTimeOffset#UtcNow instead of DateTimeOffset#Now (UtcNow is actually faster lol)

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
Octol1ttle 2023-04-27 22:14:30 +05:00
parent 41958deb0a
commit cdfa0e11f7
Signed by: Octol1ttle
GPG key ID: B77C34313AEE1FFF
8 changed files with 28 additions and 21 deletions

View file

@ -61,7 +61,7 @@ public static class Boyfriend {
private static async void TickAllGuildsAsync(object? sender, ElapsedEventArgs e) {
if (GuildTickTasks.Count is not 0) return;
var now = DateTimeOffset.Now;
var now = DateTimeOffset.UtcNow;
foreach (var guild in Client.Guilds) GuildTickTasks.Add(TickGuildAsync(guild, now));
if (now >= _nextSongAt) {

View file

@ -34,7 +34,7 @@ public sealed class BanCommand : ICommand {
var memberData = GuildData.Get(guild).MemberData[toBan.Id];
memberData.BannedUntil
= duration.TotalSeconds < 1 ? DateTimeOffset.MaxValue : DateTimeOffset.Now.Add(duration);
= duration.TotalSeconds < 1 ? DateTimeOffset.MaxValue : DateTimeOffset.UtcNow.Add(duration);
memberData.Roles.Clear();
cmd.ConfigWriteScheduled = true;

View file

@ -19,7 +19,7 @@ public sealed class MuteCommand : ICommand {
if ((role is not null && toMute.Roles.Contains(role))
|| (toMute.TimedOutUntil is not null
&& toMute.TimedOutUntil.Value
> DateTimeOffset.Now)) {
> DateTimeOffset.UtcNow)) {
cmd.Reply(Messages.MemberAlreadyMuted, ReplyEmojis.Error);
return;
}
@ -37,7 +37,7 @@ public sealed class MuteCommand : ICommand {
var memberData = data.MemberData[toMute.Id];
if (role is not null) {
memberData.MutedUntil = DateTimeOffset.Now.Add(duration);
memberData.MutedUntil = DateTimeOffset.UtcNow.Add(duration);
if (data.Preferences["RemoveRolesOnMute"] is "true") {
memberData.Roles = toMute.RoleIds.ToList();
memberData.Roles.Remove(cmd.Context.Guild.Id);

View file

@ -7,7 +7,8 @@ public sealed class PingCommand : ICommand {
var builder = Boyfriend.StringBuilder;
builder.Append(Utils.GetBeep())
.Append(Math.Round(Math.Abs(DateTimeOffset.Now.Subtract(cmd.Context.Message.Timestamp).TotalMilliseconds)))
.Append(
Math.Round(Math.Abs(DateTimeOffset.UtcNow.Subtract(cmd.Context.Message.Timestamp).TotalMilliseconds)))
.Append(Messages.Milliseconds);
cmd.Reply(builder.ToString(), ReplyEmojis.Ping);

View file

@ -15,7 +15,7 @@ public sealed class RemindCommand : ICommand {
var reminderText = cmd.GetRemaining(cleanArgs, 1, "ReminderText");
if (reminderText is not null) {
var reminderOffset = DateTimeOffset.Now.Add(remindIn);
var reminderOffset = DateTimeOffset.UtcNow.Add(remindIn);
GuildData.Get(cmd.Context.Guild).MemberData[cmd.Context.User.Id].Reminders.Add(
new Reminder {
RemindAt = reminderOffset,

View file

@ -79,7 +79,7 @@ public record GuildData {
foreach (var member in guild.Users) {
if (MemberData.TryGetValue(member.Id, out var memberData)) {
if (!memberData.IsInGuild
&& DateTimeOffset.Now.ToUnixTimeSeconds()
&& DateTimeOffset.UtcNow.ToUnixTimeSeconds()
- Math.Max(
memberData.LeftAt.Last().ToUnixTimeSeconds(),
memberData.BannedUntil?.ToUnixTimeSeconds() ?? 0)

View file

@ -70,7 +70,7 @@ public static class EventHandler {
await Task.Delay(500);
var auditLogEntry = (await guild.GetAuditLogsAsync(1).FlattenAsync()).First();
if (auditLogEntry.CreatedAt >= DateTimeOffset.Now.Subtract(TimeSpan.FromSeconds(1))
if (auditLogEntry.CreatedAt >= DateTimeOffset.UtcNow.Subtract(TimeSpan.FromSeconds(1))
&& auditLogEntry.Data is MessageDeleteAuditLogData data
&& msg.Author.Id == data.Target.Id)
mention = auditLogEntry.User.Mention;
@ -89,9 +89,10 @@ public static class EventHandler {
"whoami" => message.ReplyAsync("`nobody`"),
"сука !!" => message.ReplyAsync("`root`"),
"воооо" => message.ReplyAsync("`removing /...`"),
"пон" => message.ReplyAsync("https://cdn.discordapp.com/attachments/837385840946053181/1087236080950055023/vUORS10xPaY-1.jpg"),
"++++" => message.ReplyAsync("#"),
_ => new CommandProcessor(message).HandleCommandAsync()
"пон" => message.ReplyAsync(
"https://cdn.discordapp.com/attachments/837385840946053181/1087236080950055023/vUORS10xPaY-1.jpg"),
"++++" => message.ReplyAsync("#"),
_ => new CommandProcessor(message).HandleCommandAsync()
};
return Task.CompletedTask;
}
@ -141,7 +142,7 @@ public static class EventHandler {
memberData.JoinedAt.Add(user.JoinedAt!.Value);
}
if (DateTimeOffset.Now < memberData.MutedUntil) {
if (DateTimeOffset.UtcNow < memberData.MutedUntil) {
await user.AddRoleAsync(data.MuteRole);
if (config["RemoveRolesOnMute"] is "false" && config["ReturnRolesOnRejoin"] is "true")
await user.AddRolesAsync(memberData.Roles);
@ -151,7 +152,7 @@ public static class EventHandler {
private static Task UserLeftEvent(SocketGuild guild, SocketUser user) {
var data = GuildData.Get(guild).MemberData[user.Id];
data.IsInGuild = false;
data.LeftAt.Add(DateTimeOffset.Now);
data.LeftAt.Add(DateTimeOffset.UtcNow);
return Task.CompletedTask;
}
@ -226,6 +227,6 @@ public static class EventHandler {
await channel.SendMessageAsync(
string.Format(
Messages.EventCompleted, Utils.Wrap(scheduledEvent.Name),
Utils.GetHumanizedTimeSpan(DateTimeOffset.Now.Subtract(scheduledEvent.StartTime))));
Utils.GetHumanizedTimeSpan(DateTimeOffset.UtcNow.Subtract(scheduledEvent.StartTime))));
}
}

View file

@ -58,8 +58,10 @@ public static partial class Utils {
await channel.SendMessageAsync(text, false, null, null, allowRoles ? AllowRoles : AllowedMentions.None);
} catch (Exception e) {
await Boyfriend.Log(new LogMessage(LogSeverity.Error, nameof(Utils),
"Exception while silently sending message", e));
await Boyfriend.Log(
new LogMessage(
LogSeverity.Error, nameof(Utils),
"Exception while silently sending message", e));
}
}
@ -126,8 +128,10 @@ public static partial class Utils {
}
public static SocketTextChannel? GetEventNotificationChannel(SocketGuild guild) {
return guild.GetTextChannel(ParseMention(GuildData.Get(guild)
.Preferences["EventNotificationChannel"]));
return guild.GetTextChannel(
ParseMention(
GuildData.Get(guild)
.Preferences["EventNotificationChannel"]));
}
public static bool UserExists(ulong id) {
@ -138,8 +142,9 @@ public static partial class Utils {
return GuildData.GuildDataDictionary.Values.Any(gData => gData.MemberData.Values.Any(mData => mData.Id == id));
}
public static async Task<bool> UnmuteMemberAsync(GuildData data, string modDiscrim, SocketGuildUser toUnmute,
string reason) {
public static async Task<bool> UnmuteMemberAsync(
GuildData data, string modDiscrim, SocketGuildUser toUnmute,
string reason) {
var requestOptions = GetRequestOptions($"({modDiscrim}) {reason}");
var role = data.MuteRole;
@ -150,7 +155,7 @@ public static partial class Utils {
await toUnmute.RemoveRoleAsync(role, requestOptions);
data.MemberData[toUnmute.Id].MutedUntil = null;
} else {
if (toUnmute.TimedOutUntil is null || toUnmute.TimedOutUntil.Value < DateTimeOffset.Now) return false;
if (toUnmute.TimedOutUntil is null || toUnmute.TimedOutUntil.Value < DateTimeOffset.UtcNow) return false;
await toUnmute.RemoveTimeOutAsync(requestOptions);
}