forked from TeamInklings/Octobot
Refactor guild data storage (#13)
Co-authored-by: mctaylors <volkovvladislav8@gmail.com>
This commit is contained in:
parent
f0a6c8faff
commit
7b8888dae3
24 changed files with 941 additions and 661 deletions
|
@ -1,3 +1,4 @@
|
|||
using Boyfriend.Data;
|
||||
using Discord;
|
||||
using Discord.WebSocket;
|
||||
|
||||
|
@ -7,10 +8,10 @@ public sealed class BanCommand : ICommand {
|
|||
public string[] Aliases { get; } = { "ban", "бан" };
|
||||
|
||||
public async Task RunAsync(CommandProcessor cmd, string[] args, string[] cleanArgs) {
|
||||
var toBan = cmd.GetUser(args, cleanArgs, 0, "ToBan");
|
||||
var toBan = cmd.GetUser(args, cleanArgs, 0);
|
||||
if (toBan is null || !cmd.HasPermission(GuildPermission.BanMembers)) return;
|
||||
|
||||
var memberToBan = cmd.GetMember(toBan);
|
||||
var memberToBan = cmd.GetMember(toBan.Item1);
|
||||
if (memberToBan is not null && !cmd.CanInteractWith(memberToBan, "Ban")) return;
|
||||
|
||||
var duration = CommandProcessor.GetTimeSpan(args, 1);
|
||||
|
@ -18,21 +19,27 @@ public sealed class BanCommand : ICommand {
|
|||
if (reason is not null) await BanUserAsync(cmd, toBan, duration, reason);
|
||||
}
|
||||
|
||||
private static async Task BanUserAsync(CommandProcessor cmd, SocketUser toBan, TimeSpan duration, string reason) {
|
||||
private static async Task BanUserAsync(CommandProcessor cmd, Tuple<ulong, SocketUser?> toBan, TimeSpan duration,
|
||||
string reason) {
|
||||
var author = cmd.Context.User;
|
||||
var guild = cmd.Context.Guild;
|
||||
await Utils.SendDirectMessage(toBan,
|
||||
string.Format(Messages.YouWereBanned, author.Mention, guild.Name, Utils.Wrap(reason)));
|
||||
if (toBan.Item2 is not null)
|
||||
await Utils.SendDirectMessage(toBan.Item2,
|
||||
string.Format(Messages.YouWereBanned, author.Mention, guild.Name, Utils.Wrap(reason)));
|
||||
|
||||
var guildBanMessage = $"({author}) {reason}";
|
||||
await guild.AddBanAsync(toBan, 0, guildBanMessage);
|
||||
await guild.AddBanAsync(toBan.Item1, 0, guildBanMessage);
|
||||
|
||||
var feedback = string.Format(Messages.FeedbackUserBanned, toBan.Mention,
|
||||
Utils.GetHumanizedTimeOffset(duration), Utils.Wrap(reason));
|
||||
var memberData = GuildData.Get(guild).MemberData[toBan.Item1];
|
||||
memberData.BannedUntil
|
||||
= duration.TotalSeconds < 1 ? DateTimeOffset.MaxValue : DateTimeOffset.Now.Add(duration);
|
||||
memberData.Roles.Clear();
|
||||
|
||||
cmd.ConfigWriteScheduled = true;
|
||||
|
||||
var feedback = string.Format(Messages.FeedbackUserBanned, $"<@{toBan.Item1.ToString()}>",
|
||||
Utils.GetHumanizedTimeSpan(duration), Utils.Wrap(reason));
|
||||
cmd.Reply(feedback, ReplyEmojis.Banned);
|
||||
cmd.Audit(feedback);
|
||||
|
||||
if (duration.TotalSeconds > 0)
|
||||
await Task.FromResult(Utils.DelayedUnbanAsync(cmd, toBan.Id, Messages.PunishmentExpired, duration));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using Discord;
|
||||
using System.Diagnostics;
|
||||
using Discord;
|
||||
using Discord.WebSocket;
|
||||
|
||||
namespace Boyfriend.Commands;
|
||||
|
@ -7,7 +8,7 @@ public sealed class ClearCommand : ICommand {
|
|||
public string[] Aliases { get; } = { "clear", "purge", "очистить", "стереть" };
|
||||
|
||||
public async Task RunAsync(CommandProcessor cmd, string[] args, string[] cleanArgs) {
|
||||
if (cmd.Context.Channel is not SocketTextChannel channel) throw new Exception();
|
||||
if (cmd.Context.Channel is not SocketTextChannel channel) throw new UnreachableException();
|
||||
|
||||
if (!cmd.HasPermission(GuildPermission.ManageMessages)) return;
|
||||
|
||||
|
@ -18,6 +19,7 @@ public sealed class ClearCommand : ICommand {
|
|||
var user = (SocketGuildUser)cmd.Context.User;
|
||||
await channel.DeleteMessagesAsync(messages, Utils.GetRequestOptions(user.ToString()!));
|
||||
|
||||
cmd.Audit(string.Format(Messages.FeedbackMessagesCleared, (toDelete + 1).ToString()));
|
||||
cmd.Audit(string.Format(Messages.FeedbackMessagesCleared, (toDelete + 1).ToString(),
|
||||
Utils.MentionChannel(channel.Id)));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
using Boyfriend.Data;
|
||||
using Humanizer;
|
||||
|
||||
namespace Boyfriend.Commands;
|
||||
|
@ -6,7 +7,7 @@ public sealed class HelpCommand : ICommand {
|
|||
public string[] Aliases { get; } = { "help", "помощь", "справка" };
|
||||
|
||||
public Task RunAsync(CommandProcessor cmd, string[] args, string[] cleanArgs) {
|
||||
var prefix = Boyfriend.GetGuildConfig(cmd.Context.Guild.Id)["Prefix"];
|
||||
var prefix = GuildData.Get(cmd.Context.Guild).Preferences["Prefix"];
|
||||
var toSend = Boyfriend.StringBuilder.Append(Messages.CommandHelp);
|
||||
|
||||
foreach (var command in CommandProcessor.Commands)
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
using Boyfriend.Data;
|
||||
using Discord;
|
||||
using Discord.WebSocket;
|
||||
|
||||
|
@ -7,7 +8,7 @@ public sealed class KickCommand : ICommand {
|
|||
public string[] Aliases { get; } = { "kick", "кик", "выгнать" };
|
||||
|
||||
public async Task RunAsync(CommandProcessor cmd, string[] args, string[] cleanArgs) {
|
||||
var toKick = cmd.GetMember(args, cleanArgs, 0, "ToKick");
|
||||
var toKick = cmd.GetMember(args, 0);
|
||||
if (toKick is null || !cmd.HasPermission(GuildPermission.KickMembers)) return;
|
||||
|
||||
if (cmd.CanInteractWith(toKick, "Kick"))
|
||||
|
@ -22,6 +23,9 @@ public sealed class KickCommand : ICommand {
|
|||
string.Format(Messages.YouWereKicked, cmd.Context.User.Mention, cmd.Context.Guild.Name,
|
||||
Utils.Wrap(reason)));
|
||||
|
||||
GuildData.Get(cmd.Context.Guild).MemberData[toKick.Id].Roles.Clear();
|
||||
cmd.ConfigWriteScheduled = true;
|
||||
|
||||
await toKick.KickAsync(guildKickMessage);
|
||||
var format = string.Format(Messages.FeedbackMemberKicked, toKick.Mention, Utils.Wrap(reason));
|
||||
cmd.Reply(format, ReplyEmojis.Kicked);
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
using Boyfriend.Data;
|
||||
using Discord;
|
||||
using Discord.Net;
|
||||
using Discord.WebSocket;
|
||||
|
||||
namespace Boyfriend.Commands;
|
||||
|
@ -8,64 +8,38 @@ public sealed class MuteCommand : ICommand {
|
|||
public string[] Aliases { get; } = { "mute", "timeout", "заглушить", "мут" };
|
||||
|
||||
public async Task RunAsync(CommandProcessor cmd, string[] args, string[] cleanArgs) {
|
||||
var toMute = cmd.GetMember(args, cleanArgs, 0, "ToMute");
|
||||
var toMute = cmd.GetMember(args, 0);
|
||||
if (toMute is null) return;
|
||||
|
||||
var duration = CommandProcessor.GetTimeSpan(args, 1);
|
||||
var reason = cmd.GetRemaining(args, duration.TotalSeconds < 1 ? 1 : 2, "MuteReason");
|
||||
if (reason is null) return;
|
||||
var role = Utils.GetMuteRole(cmd.Context.Guild);
|
||||
var guildData = GuildData.Get(cmd.Context.Guild);
|
||||
var role = guildData.MuteRole;
|
||||
|
||||
if ((role is not null && toMute.Roles.Contains(role))
|
||||
|| (toMute.TimedOutUntil is not null
|
||||
&& toMute.TimedOutUntil.Value.ToUnixTimeSeconds()
|
||||
> DateTimeOffset.Now.ToUnixTimeSeconds())) {
|
||||
&& toMute.TimedOutUntil.Value
|
||||
> DateTimeOffset.Now)) {
|
||||
cmd.Reply(Messages.MemberAlreadyMuted, ReplyEmojis.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
var rolesRemoved = Boyfriend.GetRemovedRoles(cmd.Context.Guild.Id);
|
||||
|
||||
if (rolesRemoved.TryGetValue(toMute.Id, out var mutedRemovedRoles)) {
|
||||
foreach (var roleId in mutedRemovedRoles) await toMute.AddRoleAsync(roleId);
|
||||
rolesRemoved.Remove(toMute.Id);
|
||||
cmd.ConfigWriteScheduled = true;
|
||||
cmd.Reply(Messages.RolesReturned, ReplyEmojis.Warning);
|
||||
}
|
||||
|
||||
if (cmd.HasPermission(GuildPermission.ModerateMembers) && cmd.CanInteractWith(toMute, "Mute"))
|
||||
await MuteMemberAsync(cmd, toMute, duration, reason);
|
||||
await MuteMemberAsync(cmd, toMute, duration, guildData, reason);
|
||||
}
|
||||
|
||||
private static async Task MuteMemberAsync(CommandProcessor cmd, SocketGuildUser toMute,
|
||||
TimeSpan duration, string reason) {
|
||||
var guild = cmd.Context.Guild;
|
||||
var config = Boyfriend.GetGuildConfig(guild.Id);
|
||||
TimeSpan duration, GuildData data, string reason) {
|
||||
var requestOptions = Utils.GetRequestOptions($"({cmd.Context.User}) {reason}");
|
||||
var role = Utils.GetMuteRole(guild);
|
||||
var role = data.MuteRole;
|
||||
var hasDuration = duration.TotalSeconds > 0;
|
||||
|
||||
if (role is not null) {
|
||||
if (config["RemoveRolesOnMute"] is "true") {
|
||||
var rolesRemoved = new List<ulong>();
|
||||
foreach (var userRole in toMute.Roles)
|
||||
try {
|
||||
if (userRole == guild.EveryoneRole || userRole == role) continue;
|
||||
await toMute.RemoveRoleAsync(role);
|
||||
rolesRemoved.Add(userRole.Id);
|
||||
} catch (HttpException e) {
|
||||
cmd.Reply(string.Format(Messages.RoleRemovalFailed, $"<@&{userRole}>", Utils.Wrap(e.Reason)),
|
||||
ReplyEmojis.Warning);
|
||||
}
|
||||
|
||||
Boyfriend.GetRemovedRoles(guild.Id).Add(toMute.Id, rolesRemoved.AsReadOnly());
|
||||
cmd.ConfigWriteScheduled = true;
|
||||
}
|
||||
if (data.Preferences["RemoveRolesOnMute"] is "true")
|
||||
await toMute.RemoveRolesAsync(toMute.Roles, requestOptions);
|
||||
|
||||
await toMute.AddRoleAsync(role, requestOptions);
|
||||
|
||||
if (hasDuration)
|
||||
await Task.FromResult(Utils.DelayedUnmuteAsync(cmd, toMute, Messages.PunishmentExpired, duration));
|
||||
} else {
|
||||
if (!hasDuration || duration.TotalDays > 28) {
|
||||
cmd.Reply(Messages.DurationRequiredForTimeOuts, ReplyEmojis.Error);
|
||||
|
@ -80,8 +54,11 @@ public sealed class MuteCommand : ICommand {
|
|||
await toMute.SetTimeOutAsync(duration, requestOptions);
|
||||
}
|
||||
|
||||
data.MemberData[toMute.Id].MutedUntil = DateTimeOffset.Now.Add(duration);
|
||||
cmd.ConfigWriteScheduled = true;
|
||||
|
||||
var feedback = string.Format(Messages.FeedbackMemberMuted, toMute.Mention,
|
||||
Utils.GetHumanizedTimeOffset(duration),
|
||||
Utils.GetHumanizedTimeSpan(duration),
|
||||
Utils.Wrap(reason));
|
||||
cmd.Reply(feedback, ReplyEmojis.Muted);
|
||||
cmd.Audit(feedback);
|
||||
|
|
|
@ -7,7 +7,7 @@ public sealed class PingCommand : ICommand {
|
|||
var builder = Boyfriend.StringBuilder;
|
||||
|
||||
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);
|
||||
|
||||
cmd.Reply(builder.ToString(), ReplyEmojis.Ping);
|
||||
|
|
23
Boyfriend/Commands/RemindCommand.cs
Normal file
23
Boyfriend/Commands/RemindCommand.cs
Normal file
|
@ -0,0 +1,23 @@
|
|||
using Boyfriend.Data;
|
||||
|
||||
namespace Boyfriend.Commands;
|
||||
|
||||
public sealed class RemindCommand : ICommand {
|
||||
public string[] Aliases { get; } = { "remind", "reminder", "remindme", "напомни", "напомнить", "напоминание" };
|
||||
|
||||
public Task RunAsync(CommandProcessor cmd, string[] args, string[] cleanArgs) {
|
||||
// TODO: actually make this good
|
||||
var remindIn = CommandProcessor.GetTimeSpan(args, 0);
|
||||
var reminderText = cmd.GetRemaining(cleanArgs, 1, "ReminderText");
|
||||
if (reminderText is not null)
|
||||
GuildData.Get(cmd.Context.Guild).MemberData[cmd.Context.User.Id].Reminders.Add(new Reminder {
|
||||
RemindAt = DateTimeOffset.Now.Add(remindIn),
|
||||
ReminderText = reminderText,
|
||||
ReminderChannel = cmd.Context.Channel.Id
|
||||
});
|
||||
|
||||
cmd.ConfigWriteScheduled = true;
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
using Boyfriend.Data;
|
||||
using Discord;
|
||||
|
||||
namespace Boyfriend.Commands;
|
||||
|
@ -9,14 +10,17 @@ public sealed class SettingsCommand : ICommand {
|
|||
if (!cmd.HasPermission(GuildPermission.ManageGuild)) return Task.CompletedTask;
|
||||
|
||||
var guild = cmd.Context.Guild;
|
||||
var config = Boyfriend.GetGuildConfig(guild.Id);
|
||||
var data = GuildData.Get(guild);
|
||||
var config = data.Preferences;
|
||||
|
||||
if (args.Length is 0) {
|
||||
var currentSettings = Boyfriend.StringBuilder.AppendLine(Messages.CurrentSettings);
|
||||
|
||||
foreach (var setting in Boyfriend.DefaultConfig) {
|
||||
foreach (var setting in GuildData.DefaultPreferences) {
|
||||
var format = "{0}";
|
||||
var currentValue = config[setting.Key] is "default" ? Messages.DefaultWelcomeMessage : config[setting.Key];
|
||||
var currentValue = config[setting.Key] is "default"
|
||||
? Messages.DefaultWelcomeMessage
|
||||
: config[setting.Key];
|
||||
|
||||
if (setting.Key.EndsWith("Channel")) {
|
||||
if (guild.GetTextChannel(ulong.Parse(currentValue)) is not null) format = "<#{0}>";
|
||||
|
@ -41,10 +45,7 @@ public sealed class SettingsCommand : ICommand {
|
|||
var selectedSetting = args[0].ToLower();
|
||||
|
||||
var exists = false;
|
||||
// ReSharper disable once ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator
|
||||
// Too many allocations
|
||||
foreach (var setting in Boyfriend.DefaultConfig.Keys) {
|
||||
if (selectedSetting != setting.ToLower()) continue;
|
||||
foreach (var setting in GuildData.DefaultPreferences.Keys.Where(x => x.ToLower() == selectedSetting)) {
|
||||
selectedSetting = setting;
|
||||
exists = true;
|
||||
break;
|
||||
|
@ -70,7 +71,7 @@ public sealed class SettingsCommand : ICommand {
|
|||
}
|
||||
} else { value = "reset"; }
|
||||
|
||||
if (IsBool(Boyfriend.DefaultConfig[selectedSetting]) && !IsBool(value)) {
|
||||
if (IsBool(GuildData.DefaultPreferences[selectedSetting]) && !IsBool(value)) {
|
||||
value = value switch {
|
||||
"y" or "yes" or "д" or "да" => "true",
|
||||
"n" or "no" or "н" or "нет" => "false",
|
||||
|
@ -95,14 +96,14 @@ public sealed class SettingsCommand : ICommand {
|
|||
|
||||
var formattedValue = selectedSetting switch {
|
||||
"WelcomeMessage" => Utils.Wrap(Messages.DefaultWelcomeMessage),
|
||||
"EventStartedReceivers" => Utils.Wrap(Boyfriend.DefaultConfig[selectedSetting])!,
|
||||
"EventStartedReceivers" => Utils.Wrap(GuildData.DefaultPreferences[selectedSetting])!,
|
||||
_ => value is "reset" or "default" ? Messages.SettingNotDefined
|
||||
: IsBool(value) ? YesOrNo(value is "true")
|
||||
: string.Format(formatting, value)
|
||||
};
|
||||
|
||||
if (value is "reset" or "default") {
|
||||
config[selectedSetting] = Boyfriend.DefaultConfig[selectedSetting];
|
||||
config[selectedSetting] = GuildData.DefaultPreferences[selectedSetting];
|
||||
} else {
|
||||
if (value == config[selectedSetting]) {
|
||||
cmd.Reply(string.Format(Messages.SettingsNothingChanged, localizedSelectedSetting, formattedValue),
|
||||
|
@ -129,13 +130,28 @@ public sealed class SettingsCommand : ICommand {
|
|||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
if (selectedSetting is "MuteRole") Utils.RemoveMuteRoleFromCache(ulong.Parse(config[selectedSetting]));
|
||||
if (selectedSetting.EndsWith("Offset") && !int.TryParse(value, out _)) {
|
||||
cmd.Reply(Messages.InvalidSettingValue, ReplyEmojis.Error);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
switch (selectedSetting) {
|
||||
case "MuteRole":
|
||||
data.MuteRole = guild.GetRole(mention);
|
||||
break;
|
||||
case "PublicFeedbackChannel":
|
||||
data.PublicFeedbackChannel = guild.GetTextChannel(mention);
|
||||
break;
|
||||
case "PrivateFeedbackChannel":
|
||||
data.PrivateFeedbackChannel = guild.GetTextChannel(mention);
|
||||
break;
|
||||
}
|
||||
|
||||
config[selectedSetting] = value;
|
||||
}
|
||||
|
||||
if (selectedSetting is "Lang") {
|
||||
Utils.SetCurrentLanguage(guild.Id);
|
||||
Utils.SetCurrentLanguage(guild);
|
||||
localizedSelectedSetting = Utils.GetMessage($"Settings{selectedSetting}");
|
||||
}
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@ public sealed class UnbanCommand : ICommand {
|
|||
if (reason is not null) await UnbanUserAsync(cmd, id.Value, reason);
|
||||
}
|
||||
|
||||
public static async Task UnbanUserAsync(CommandProcessor cmd, ulong id, string reason) {
|
||||
private static async Task UnbanUserAsync(CommandProcessor cmd, ulong id, string reason) {
|
||||
var requestOptions = Utils.GetRequestOptions($"({cmd.Context.User}) {reason}");
|
||||
await cmd.Context.Guild.RemoveBanAsync(id, requestOptions);
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
using Boyfriend.Data;
|
||||
using Discord;
|
||||
using Discord.WebSocket;
|
||||
|
||||
|
@ -9,38 +10,25 @@ public sealed class UnmuteCommand : ICommand {
|
|||
public async Task RunAsync(CommandProcessor cmd, string[] args, string[] cleanArgs) {
|
||||
if (!cmd.HasPermission(GuildPermission.ModerateMembers)) return;
|
||||
|
||||
var toUnmute = cmd.GetMember(args, cleanArgs, 0, "ToUnmute");
|
||||
var toUnmute = cmd.GetMember(args, 0);
|
||||
if (toUnmute is null) return;
|
||||
var reason = cmd.GetRemaining(args, 1, "UnmuteReason");
|
||||
if (reason is not null && cmd.CanInteractWith(toUnmute, "Unmute"))
|
||||
await UnmuteMemberAsync(cmd, toUnmute, reason);
|
||||
}
|
||||
|
||||
public static async Task UnmuteMemberAsync(CommandProcessor cmd, SocketGuildUser toUnmute,
|
||||
private static async Task UnmuteMemberAsync(CommandProcessor cmd, SocketGuildUser toUnmute,
|
||||
string reason) {
|
||||
var requestOptions = Utils.GetRequestOptions($"({cmd.Context.User}) {reason}");
|
||||
var role = Utils.GetMuteRole(cmd.Context.Guild);
|
||||
var isMuted = await Utils.UnmuteMemberAsync(GuildData.Get(cmd.Context.Guild), cmd.Context.User.ToString(),
|
||||
toUnmute, reason);
|
||||
|
||||
if (role is not null && toUnmute.Roles.Contains(role)) {
|
||||
var rolesRemoved = Boyfriend.GetRemovedRoles(cmd.Context.Guild.Id);
|
||||
|
||||
if (rolesRemoved.TryGetValue(toUnmute.Id, out var unmutedRemovedRoles)) {
|
||||
await toUnmute.AddRolesAsync(unmutedRemovedRoles);
|
||||
rolesRemoved.Remove(toUnmute.Id);
|
||||
cmd.ConfigWriteScheduled = true;
|
||||
}
|
||||
|
||||
await toUnmute.RemoveRoleAsync(role, requestOptions);
|
||||
} else {
|
||||
if (toUnmute.TimedOutUntil is null || toUnmute.TimedOutUntil.Value.ToUnixTimeSeconds() <
|
||||
DateTimeOffset.Now.ToUnixTimeSeconds()) {
|
||||
cmd.Reply(Messages.MemberNotMuted, ReplyEmojis.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
await toUnmute.RemoveTimeOutAsync();
|
||||
if (!isMuted) {
|
||||
cmd.Reply(Messages.MemberNotMuted, ReplyEmojis.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
cmd.ConfigWriteScheduled = true;
|
||||
|
||||
var feedback = string.Format(Messages.FeedbackMemberUnmuted, toUnmute.Mention, Utils.Wrap(reason));
|
||||
cmd.Reply(feedback, ReplyEmojis.Unmuted);
|
||||
cmd.Audit(feedback);
|
||||
|
|
Reference in a new issue