1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-04-20 00:43:36 +03:00

Fix more bugs and problems

This commit is contained in:
Octol1ttle 2023-01-18 11:48:38 +05:00
parent f1bd54d6f6
commit 7059c7576d
Signed by: Octol1ttle
GPG key ID: B77C34313AEE1FFF
8 changed files with 46 additions and 19 deletions

View file

@ -16,6 +16,7 @@ public static class Boyfriend {
GatewayIntents GatewayIntents
= (GatewayIntents.AllUnprivileged | GatewayIntents.MessageContent | GatewayIntents.GuildMembers) & = (GatewayIntents.AllUnprivileged | GatewayIntents.MessageContent | GatewayIntents.GuildMembers) &
~GatewayIntents.GuildInvites, ~GatewayIntents.GuildInvites,
AlwaysDownloadUsers = true,
AlwaysResolveStickers = false, AlwaysResolveStickers = false,
AlwaysDownloadDefaultStickers = false, AlwaysDownloadDefaultStickers = false,
LargeThreshold = 500 LargeThreshold = 500
@ -74,8 +75,8 @@ public static class Boyfriend {
try { Task.WaitAll(GuildTickTasks.ToArray()); } catch (AggregateException ex) { try { Task.WaitAll(GuildTickTasks.ToArray()); } catch (AggregateException ex) {
foreach (var exc in ex.InnerExceptions) foreach (var exc in ex.InnerExceptions)
await Log(new LogMessage(LogSeverity.Error, nameof(CommandProcessor), await Log(new LogMessage(LogSeverity.Error, nameof(Boyfriend),
"Exception while executing commands", exc)); "Exception while ticking guilds", exc));
} }
GuildTickTasks.Clear(); GuildTickTasks.Clear();
@ -114,10 +115,11 @@ public static class Boyfriend {
var saveData = false; var saveData = false;
_ = int.TryParse(config["EventEarlyNotificationOffset"], out var offset); _ = int.TryParse(config["EventEarlyNotificationOffset"], out var offset);
foreach (var schEvent in guild.Events) foreach (var schEvent in guild.Events)
if (config["AutoStartEvents"] is "true" && DateTimeOffset.Now >= schEvent.StartTime) { if (schEvent.Status is GuildScheduledEventStatus.Scheduled && config["AutoStartEvents"] is "true" &&
await schEvent.StartAsync(); DateTimeOffset.Now >= schEvent.StartTime) { await schEvent.StartAsync(); } else if
} else if (!data.EarlyNotifications.Contains(schEvent.Id) && (!data.EarlyNotifications.Contains(schEvent.Id) &&
DateTimeOffset.Now >= schEvent.StartTime.Subtract(new TimeSpan(0, offset, 0))) { DateTimeOffset.Now >= schEvent.StartTime.Subtract(new TimeSpan(0, offset, 0))) {
data.EarlyNotifications.Add(schEvent.Id);
var receivers = config["EventStartedReceivers"]; var receivers = config["EventStartedReceivers"];
var role = guild.GetRole(ulong.Parse(config["EventNotificationRole"])); var role = guild.GetRole(ulong.Parse(config["EventNotificationRole"]));
var mentions = StringBuilder; var mentions = StringBuilder;
@ -128,12 +130,12 @@ public static class Boyfriend {
.Where(user => role is null || !((RestGuildUser)user).RoleIds.Contains(role.Id)) .Where(user => role is null || !((RestGuildUser)user).RoleIds.Contains(role.Id))
.Aggregate(mentions, (current, user) => current.Append($"{user.Mention} ")); .Aggregate(mentions, (current, user) => current.Append($"{user.Mention} "));
await Utils.GetEventNotificationChannel(guild)?.SendMessageAsync(string.Format(Messages.EventStarted, await Utils.GetEventNotificationChannel(guild)?.SendMessageAsync(string.Format(
Messages.EventEarlyNotification,
mentions, mentions,
Utils.Wrap(schEvent.Name), Utils.Wrap(schEvent.Name),
Utils.Wrap(schEvent.Location) ?? Utils.MentionChannel(schEvent.Channel.Id)))!; schEvent.StartTime.ToUnixTimeSeconds().ToString()))!;
mentions.Clear(); mentions.Clear();
data.EarlyNotifications.Add(schEvent.Id);
} }
foreach (var mData in data.MemberData.Values) { foreach (var mData in data.MemberData.Values) {

View file

@ -36,7 +36,7 @@ public sealed class CommandProcessor {
Utils.SetCurrentLanguage(guild); Utils.SetCurrentLanguage(guild);
if (GetMember().Roles.Contains(data.MuteRole)) { if (GetMember().Roles.Contains(data.MuteRole)) {
_ = Context.Message.ReplyAsync(Messages.UserCannotUnmuteThemselves); _ = Context.Message.DeleteAsync();
return; return;
} }
@ -175,7 +175,7 @@ public sealed class CommandProcessor {
} }
var member = Context.Guild.GetUser(Utils.ParseMention(args[index])); var member = Context.Guild.GetUser(Utils.ParseMention(args[index]));
if (member is null && argument is not null) if (member is null)
Utils.SafeAppendToBuilder(_stackedReplyMessage, Utils.SafeAppendToBuilder(_stackedReplyMessage,
$"{ReplyEmojis.InvalidArgument} {Messages.InvalidMember}", $"{ReplyEmojis.InvalidArgument} {Messages.InvalidMember}",
Context.Message); Context.Message);

View file

@ -38,7 +38,7 @@ public sealed class BanCommand : ICommand {
cmd.ConfigWriteScheduled = true; cmd.ConfigWriteScheduled = true;
var feedback = string.Format(Messages.FeedbackUserBanned, $"<@{toBan.Item1.ToString()}>", var feedback = string.Format(Messages.FeedbackUserBanned, $"<@{toBan.Item1.ToString()}>",
Utils.GetHumanizedTimeOffset(duration), Utils.Wrap(reason)); Utils.GetHumanizedTimeSpan(duration), Utils.Wrap(reason));
cmd.Reply(feedback, ReplyEmojis.Banned); cmd.Reply(feedback, ReplyEmojis.Banned);
cmd.Audit(feedback); cmd.Audit(feedback);
} }

View file

@ -58,7 +58,7 @@ public sealed class MuteCommand : ICommand {
cmd.ConfigWriteScheduled = true; cmd.ConfigWriteScheduled = true;
var feedback = string.Format(Messages.FeedbackMemberMuted, toMute.Mention, var feedback = string.Format(Messages.FeedbackMemberMuted, toMute.Mention,
Utils.GetHumanizedTimeOffset(duration), Utils.GetHumanizedTimeSpan(duration),
Utils.Wrap(reason)); Utils.Wrap(reason));
cmd.Reply(feedback, ReplyEmojis.Muted); cmd.Reply(feedback, ReplyEmojis.Muted);
cmd.Audit(feedback); cmd.Audit(feedback);

View file

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

View file

@ -1,4 +1,5 @@
using Discord; using System.Text.Json.Serialization;
using Discord;
namespace Boyfriend.Data; namespace Boyfriend.Data;
@ -12,12 +13,26 @@ public record MemberData {
public List<Reminder> Reminders; public List<Reminder> Reminders;
public List<ulong> Roles; public List<ulong> Roles;
[JsonConstructor]
public MemberData(DateTimeOffset? bannedUntil, ulong id, bool isInGuild, List<DateTimeOffset> joinedAt,
List<DateTimeOffset> leftAt, DateTimeOffset? mutedUntil, List<Reminder> reminders, List<ulong> roles) {
BannedUntil = bannedUntil;
Id = id;
IsInGuild = isInGuild;
JoinedAt = joinedAt;
LeftAt = leftAt;
MutedUntil = mutedUntil;
Reminders = reminders;
Roles = roles;
}
public MemberData(IGuildUser user) { public MemberData(IGuildUser user) {
Id = user.Id; Id = user.Id;
IsInGuild = true; IsInGuild = true;
JoinedAt = new List<DateTimeOffset> { user.JoinedAt!.Value }; JoinedAt = new List<DateTimeOffset> { user.JoinedAt!.Value };
LeftAt = new List<DateTimeOffset>(); LeftAt = new List<DateTimeOffset>();
Roles = user.RoleIds.ToList(); Roles = user.RoleIds.ToList();
Roles.Remove(user.Guild.Id);
Reminders = new List<Reminder>(); Reminders = new List<Reminder>();
} }
} }

View file

@ -25,7 +25,9 @@ public static class EventHandler {
} }
private static Task RolesUpdatedEvent(Cacheable<SocketGuildUser, ulong> oldUser, SocketGuildUser newUser) { private static Task RolesUpdatedEvent(Cacheable<SocketGuildUser, ulong> oldUser, SocketGuildUser newUser) {
GuildData.Get(newUser.Guild).MemberData[newUser.Id].Roles = ((IGuildUser)newUser).RoleIds.ToList(); var data = GuildData.Get(newUser.Guild).MemberData[newUser.Id];
data.Roles = ((IGuildUser)newUser).RoleIds.ToList();
data.Roles.Remove(newUser.Guild.Id);
return Task.CompletedTask; return Task.CompletedTask;
} }
@ -205,6 +207,6 @@ public static class EventHandler {
Utils.SetCurrentLanguage(guild); Utils.SetCurrentLanguage(guild);
if (channel is not null) if (channel is not null)
await channel.SendMessageAsync(string.Format(Messages.EventCompleted, Utils.Wrap(scheduledEvent.Name), await channel.SendMessageAsync(string.Format(Messages.EventCompleted, Utils.Wrap(scheduledEvent.Name),
Utils.GetHumanizedTimeOffset(DateTimeOffset.Now.Subtract(scheduledEvent.StartTime)))); Utils.GetHumanizedTimeSpan(DateTimeOffset.Now.Subtract(scheduledEvent.StartTime))));
} }
} }

View file

@ -96,7 +96,7 @@ public static partial class Utils {
if (sendPublic && systemChannel is not null) await SilentSendAsync(systemChannel, toSend); if (sendPublic && systemChannel is not null) await SilentSendAsync(systemChannel, toSend);
} }
public static string GetHumanizedTimeOffset(TimeSpan span) { public static string GetHumanizedTimeSpan(TimeSpan span) {
return span.TotalSeconds < 1 return span.TotalSeconds < 1
? Messages.Ever ? Messages.Ever
: $" {span.Humanize(2, minUnit: TimeUnit.Second, maxUnit: TimeUnit.Month, culture: Messages.Culture.Name.Contains("RU") ? CultureInfoCache["ru"] : Messages.Culture)}"; : $" {span.Humanize(2, minUnit: TimeUnit.Second, maxUnit: TimeUnit.Month, culture: Messages.Culture.Name.Contains("RU") ? CultureInfoCache["ru"] : Messages.Culture)}";
@ -145,6 +145,7 @@ public static partial class Utils {
if (role is not null) { if (role is not null) {
if (!toUnmute.Roles.Contains(role)) return false; if (!toUnmute.Roles.Contains(role)) return false;
if (data.Preferences["RemoveRolesOnMute"] is "true")
await toUnmute.AddRolesAsync(data.MemberData[toUnmute.Id].Roles, requestOptions); await toUnmute.AddRolesAsync(data.MemberData[toUnmute.Id].Roles, requestOptions);
await toUnmute.RemoveRoleAsync(role, requestOptions); await toUnmute.RemoveRoleAsync(role, requestOptions);
data.MemberData[toUnmute.Id].MutedUntil = null; data.MemberData[toUnmute.Id].MutedUntil = null;
@ -157,6 +158,13 @@ public static partial class Utils {
return true; return true;
} }
public static async Task ReturnRolesAsync(SocketGuildUser user, List<ulong> roles) {
// ReSharper disable once ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator
foreach (var role in roles)
if (role != user.Guild.Id)
await user.AddRoleAsync(role);
}
[GeneratedRegex("[^0-9]")] [GeneratedRegex("[^0-9]")]
private static partial Regex NumbersOnlyRegex(); private static partial Regex NumbersOnlyRegex();
} }