mirror of
https://github.com/TeamOctolings/Octobot.git
synced 2025-05-10 16:03:15 +03:00
Begin adapting code to new guild data storage
This commit is contained in:
parent
f5a81fba35
commit
163e3ac46b
8 changed files with 56 additions and 167 deletions
|
@ -1,3 +1,4 @@
|
|||
using Boyfriend.Data;
|
||||
using Discord;
|
||||
using Discord.WebSocket;
|
||||
|
||||
|
@ -32,7 +33,7 @@ public sealed class BanCommand : ICommand {
|
|||
cmd.Reply(feedback, ReplyEmojis.Banned);
|
||||
cmd.Audit(feedback);
|
||||
|
||||
if (duration.TotalSeconds > 0)
|
||||
await Task.FromResult(Utils.DelayedUnbanAsync(cmd, toBan.Id, Messages.PunishmentExpired, duration));
|
||||
GuildData.FromSocketGuild(guild).MemberData[toBan.Id].BannedUntil
|
||||
= DateTimeOffset.Now.Add(duration).ToUnixTimeSeconds();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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.FromSocketGuild(cmd.Context.Guild).Preferences["Prefix"];
|
||||
var toSend = Boyfriend.StringBuilder.Append(Messages.CommandHelp);
|
||||
|
||||
foreach (var command in CommandProcessor.Commands)
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
using Boyfriend.Data;
|
||||
using Discord;
|
||||
using Discord.Net;
|
||||
using Discord.WebSocket;
|
||||
|
||||
namespace Boyfriend.Commands;
|
||||
|
@ -14,7 +14,8 @@ public sealed class MuteCommand : ICommand {
|
|||
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.FromSocketGuild(cmd.Context.Guild);
|
||||
var role = guildData.MuteRole;
|
||||
|
||||
if ((role is not null && toMute.Roles.Contains(role))
|
||||
|| (toMute.TimedOutUntil is not null
|
||||
|
@ -24,48 +25,22 @@ public sealed class MuteCommand : ICommand {
|
|||
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);
|
||||
|
||||
await toMute.AddRoleAsync(role, requestOptions);
|
||||
|
||||
if (hasDuration)
|
||||
await Task.FromResult(Utils.DelayedUnmuteAsync(cmd, toMute, Messages.PunishmentExpired, duration));
|
||||
data.MemberData[toMute.Id].MutedUntil = DateTimeOffset.Now.Add(duration).ToUnixTimeSeconds();
|
||||
} else {
|
||||
if (!hasDuration || duration.TotalDays > 28) {
|
||||
cmd.Reply(Messages.DurationRequiredForTimeOuts, ReplyEmojis.Error);
|
||||
|
|
|
@ -10,7 +10,8 @@ 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.FromSocketGuild(guild);
|
||||
var config = data.Preferences;
|
||||
|
||||
if (args.Length is 0) {
|
||||
var currentSettings = Boyfriend.StringBuilder.AppendLine(Messages.CurrentSettings);
|
||||
|
@ -132,7 +133,7 @@ public sealed class SettingsCommand : ICommand {
|
|||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
if (selectedSetting is "MuteRole") Utils.RemoveMuteRoleFromCache(ulong.Parse(config[selectedSetting]));
|
||||
if (selectedSetting is "MuteRole") data.MuteRole = guild.GetRole(mention);
|
||||
|
||||
config[selectedSetting] = value;
|
||||
}
|
||||
|
@ -158,4 +159,3 @@ public sealed class SettingsCommand : ICommand {
|
|||
return value is "true" or "false";
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
using Boyfriend.Data;
|
||||
using Discord;
|
||||
using Discord.WebSocket;
|
||||
|
||||
|
@ -19,18 +20,10 @@ public sealed class UnmuteCommand : ICommand {
|
|||
public 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 role = GuildData.FromSocketGuild(cmd.Context.Guild).MuteRole;
|
||||
|
||||
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);
|
||||
// TODO: Return roles
|
||||
} else {
|
||||
if (toUnmute.TimedOutUntil is null || toUnmute.TimedOutUntil.Value.ToUnixTimeSeconds() <
|
||||
DateTimeOffset.Now.ToUnixTimeSeconds()) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue