mirror of
https://github.com/TeamOctolings/Octobot.git
synced 2025-01-31 09:09:00 +03:00
Use DateTimeOffset#UtcNow instead of DateTimeOffset#Now (UtcNow is actually faster lol)
Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
parent
41958deb0a
commit
cdfa0e11f7
8 changed files with 28 additions and 21 deletions
|
@ -61,7 +61,7 @@ public static class Boyfriend {
|
||||||
private static async void TickAllGuildsAsync(object? sender, ElapsedEventArgs e) {
|
private static async void TickAllGuildsAsync(object? sender, ElapsedEventArgs e) {
|
||||||
if (GuildTickTasks.Count is not 0) return;
|
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));
|
foreach (var guild in Client.Guilds) GuildTickTasks.Add(TickGuildAsync(guild, now));
|
||||||
|
|
||||||
if (now >= _nextSongAt) {
|
if (now >= _nextSongAt) {
|
||||||
|
|
|
@ -34,7 +34,7 @@ public sealed class BanCommand : ICommand {
|
||||||
|
|
||||||
var memberData = GuildData.Get(guild).MemberData[toBan.Id];
|
var memberData = GuildData.Get(guild).MemberData[toBan.Id];
|
||||||
memberData.BannedUntil
|
memberData.BannedUntil
|
||||||
= duration.TotalSeconds < 1 ? DateTimeOffset.MaxValue : DateTimeOffset.Now.Add(duration);
|
= duration.TotalSeconds < 1 ? DateTimeOffset.MaxValue : DateTimeOffset.UtcNow.Add(duration);
|
||||||
memberData.Roles.Clear();
|
memberData.Roles.Clear();
|
||||||
|
|
||||||
cmd.ConfigWriteScheduled = true;
|
cmd.ConfigWriteScheduled = true;
|
||||||
|
|
|
@ -19,7 +19,7 @@ public sealed class MuteCommand : ICommand {
|
||||||
if ((role is not null && toMute.Roles.Contains(role))
|
if ((role is not null && toMute.Roles.Contains(role))
|
||||||
|| (toMute.TimedOutUntil is not null
|
|| (toMute.TimedOutUntil is not null
|
||||||
&& toMute.TimedOutUntil.Value
|
&& toMute.TimedOutUntil.Value
|
||||||
> DateTimeOffset.Now)) {
|
> DateTimeOffset.UtcNow)) {
|
||||||
cmd.Reply(Messages.MemberAlreadyMuted, ReplyEmojis.Error);
|
cmd.Reply(Messages.MemberAlreadyMuted, ReplyEmojis.Error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -37,7 +37,7 @@ public sealed class MuteCommand : ICommand {
|
||||||
var memberData = data.MemberData[toMute.Id];
|
var memberData = data.MemberData[toMute.Id];
|
||||||
|
|
||||||
if (role is not null) {
|
if (role is not null) {
|
||||||
memberData.MutedUntil = DateTimeOffset.Now.Add(duration);
|
memberData.MutedUntil = DateTimeOffset.UtcNow.Add(duration);
|
||||||
if (data.Preferences["RemoveRolesOnMute"] is "true") {
|
if (data.Preferences["RemoveRolesOnMute"] is "true") {
|
||||||
memberData.Roles = toMute.RoleIds.ToList();
|
memberData.Roles = toMute.RoleIds.ToList();
|
||||||
memberData.Roles.Remove(cmd.Context.Guild.Id);
|
memberData.Roles.Remove(cmd.Context.Guild.Id);
|
||||||
|
|
|
@ -7,7 +7,8 @@ public sealed class PingCommand : ICommand {
|
||||||
var builder = Boyfriend.StringBuilder;
|
var builder = Boyfriend.StringBuilder;
|
||||||
|
|
||||||
builder.Append(Utils.GetBeep())
|
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);
|
.Append(Messages.Milliseconds);
|
||||||
|
|
||||||
cmd.Reply(builder.ToString(), ReplyEmojis.Ping);
|
cmd.Reply(builder.ToString(), ReplyEmojis.Ping);
|
||||||
|
|
|
@ -15,7 +15,7 @@ public sealed class RemindCommand : ICommand {
|
||||||
|
|
||||||
var reminderText = cmd.GetRemaining(cleanArgs, 1, "ReminderText");
|
var reminderText = cmd.GetRemaining(cleanArgs, 1, "ReminderText");
|
||||||
if (reminderText is not null) {
|
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(
|
GuildData.Get(cmd.Context.Guild).MemberData[cmd.Context.User.Id].Reminders.Add(
|
||||||
new Reminder {
|
new Reminder {
|
||||||
RemindAt = reminderOffset,
|
RemindAt = reminderOffset,
|
||||||
|
|
|
@ -79,7 +79,7 @@ public record GuildData {
|
||||||
foreach (var member in guild.Users) {
|
foreach (var member in guild.Users) {
|
||||||
if (MemberData.TryGetValue(member.Id, out var memberData)) {
|
if (MemberData.TryGetValue(member.Id, out var memberData)) {
|
||||||
if (!memberData.IsInGuild
|
if (!memberData.IsInGuild
|
||||||
&& DateTimeOffset.Now.ToUnixTimeSeconds()
|
&& DateTimeOffset.UtcNow.ToUnixTimeSeconds()
|
||||||
- Math.Max(
|
- Math.Max(
|
||||||
memberData.LeftAt.Last().ToUnixTimeSeconds(),
|
memberData.LeftAt.Last().ToUnixTimeSeconds(),
|
||||||
memberData.BannedUntil?.ToUnixTimeSeconds() ?? 0)
|
memberData.BannedUntil?.ToUnixTimeSeconds() ?? 0)
|
||||||
|
|
|
@ -70,7 +70,7 @@ public static class EventHandler {
|
||||||
await Task.Delay(500);
|
await Task.Delay(500);
|
||||||
|
|
||||||
var auditLogEntry = (await guild.GetAuditLogsAsync(1).FlattenAsync()).First();
|
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
|
&& auditLogEntry.Data is MessageDeleteAuditLogData data
|
||||||
&& msg.Author.Id == data.Target.Id)
|
&& msg.Author.Id == data.Target.Id)
|
||||||
mention = auditLogEntry.User.Mention;
|
mention = auditLogEntry.User.Mention;
|
||||||
|
@ -89,7 +89,8 @@ public static class EventHandler {
|
||||||
"whoami" => message.ReplyAsync("`nobody`"),
|
"whoami" => message.ReplyAsync("`nobody`"),
|
||||||
"сука !!" => message.ReplyAsync("`root`"),
|
"сука !!" => message.ReplyAsync("`root`"),
|
||||||
"воооо" => message.ReplyAsync("`removing /...`"),
|
"воооо" => message.ReplyAsync("`removing /...`"),
|
||||||
"пон" => message.ReplyAsync("https://cdn.discordapp.com/attachments/837385840946053181/1087236080950055023/vUORS10xPaY-1.jpg"),
|
"пон" => message.ReplyAsync(
|
||||||
|
"https://cdn.discordapp.com/attachments/837385840946053181/1087236080950055023/vUORS10xPaY-1.jpg"),
|
||||||
"++++" => message.ReplyAsync("#"),
|
"++++" => message.ReplyAsync("#"),
|
||||||
_ => new CommandProcessor(message).HandleCommandAsync()
|
_ => new CommandProcessor(message).HandleCommandAsync()
|
||||||
};
|
};
|
||||||
|
@ -141,7 +142,7 @@ public static class EventHandler {
|
||||||
memberData.JoinedAt.Add(user.JoinedAt!.Value);
|
memberData.JoinedAt.Add(user.JoinedAt!.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (DateTimeOffset.Now < memberData.MutedUntil) {
|
if (DateTimeOffset.UtcNow < memberData.MutedUntil) {
|
||||||
await user.AddRoleAsync(data.MuteRole);
|
await user.AddRoleAsync(data.MuteRole);
|
||||||
if (config["RemoveRolesOnMute"] is "false" && config["ReturnRolesOnRejoin"] is "true")
|
if (config["RemoveRolesOnMute"] is "false" && config["ReturnRolesOnRejoin"] is "true")
|
||||||
await user.AddRolesAsync(memberData.Roles);
|
await user.AddRolesAsync(memberData.Roles);
|
||||||
|
@ -151,7 +152,7 @@ public static class EventHandler {
|
||||||
private static Task UserLeftEvent(SocketGuild guild, SocketUser user) {
|
private static Task UserLeftEvent(SocketGuild guild, SocketUser user) {
|
||||||
var data = GuildData.Get(guild).MemberData[user.Id];
|
var data = GuildData.Get(guild).MemberData[user.Id];
|
||||||
data.IsInGuild = false;
|
data.IsInGuild = false;
|
||||||
data.LeftAt.Add(DateTimeOffset.Now);
|
data.LeftAt.Add(DateTimeOffset.UtcNow);
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -226,6 +227,6 @@ public static class EventHandler {
|
||||||
await channel.SendMessageAsync(
|
await channel.SendMessageAsync(
|
||||||
string.Format(
|
string.Format(
|
||||||
Messages.EventCompleted, Utils.Wrap(scheduledEvent.Name),
|
Messages.EventCompleted, Utils.Wrap(scheduledEvent.Name),
|
||||||
Utils.GetHumanizedTimeSpan(DateTimeOffset.Now.Subtract(scheduledEvent.StartTime))));
|
Utils.GetHumanizedTimeSpan(DateTimeOffset.UtcNow.Subtract(scheduledEvent.StartTime))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
13
Utils.cs
13
Utils.cs
|
@ -58,7 +58,9 @@ public static partial class Utils {
|
||||||
|
|
||||||
await channel.SendMessageAsync(text, false, null, null, allowRoles ? AllowRoles : AllowedMentions.None);
|
await channel.SendMessageAsync(text, false, null, null, allowRoles ? AllowRoles : AllowedMentions.None);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
await Boyfriend.Log(new LogMessage(LogSeverity.Error, nameof(Utils),
|
await Boyfriend.Log(
|
||||||
|
new LogMessage(
|
||||||
|
LogSeverity.Error, nameof(Utils),
|
||||||
"Exception while silently sending message", e));
|
"Exception while silently sending message", e));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -126,7 +128,9 @@ public static partial class Utils {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SocketTextChannel? GetEventNotificationChannel(SocketGuild guild) {
|
public static SocketTextChannel? GetEventNotificationChannel(SocketGuild guild) {
|
||||||
return guild.GetTextChannel(ParseMention(GuildData.Get(guild)
|
return guild.GetTextChannel(
|
||||||
|
ParseMention(
|
||||||
|
GuildData.Get(guild)
|
||||||
.Preferences["EventNotificationChannel"]));
|
.Preferences["EventNotificationChannel"]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,7 +142,8 @@ public static partial class Utils {
|
||||||
return GuildData.GuildDataDictionary.Values.Any(gData => gData.MemberData.Values.Any(mData => mData.Id == id));
|
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,
|
public static async Task<bool> UnmuteMemberAsync(
|
||||||
|
GuildData data, string modDiscrim, SocketGuildUser toUnmute,
|
||||||
string reason) {
|
string reason) {
|
||||||
var requestOptions = GetRequestOptions($"({modDiscrim}) {reason}");
|
var requestOptions = GetRequestOptions($"({modDiscrim}) {reason}");
|
||||||
var role = data.MuteRole;
|
var role = data.MuteRole;
|
||||||
|
@ -150,7 +155,7 @@ public static partial class Utils {
|
||||||
await toUnmute.RemoveRoleAsync(role, requestOptions);
|
await toUnmute.RemoveRoleAsync(role, requestOptions);
|
||||||
data.MemberData[toUnmute.Id].MutedUntil = null;
|
data.MemberData[toUnmute.Id].MutedUntil = null;
|
||||||
} else {
|
} 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);
|
await toUnmute.RemoveTimeOutAsync(requestOptions);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue