From 313ca2674e13a179202d8c0701e814f7b43ef7bd Mon Sep 17 00:00:00 2001 From: Kwoth Date: Sat, 11 Feb 2023 22:32:36 +0100 Subject: [PATCH] timed ban should now work with userids (users who are not in the server yet), closes #400 --- .../Administration/Mute/MuteService.cs | 10 +++++----- .../UserPunish/UserPunishCommands.cs | 20 ++++++++++++++----- .../UserPunish/UserPunishService.cs | 2 +- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/NadekoBot/Modules/Administration/Mute/MuteService.cs b/src/NadekoBot/Modules/Administration/Mute/MuteService.cs index c4ff770d3..855dde0e9 100644 --- a/src/NadekoBot/Modules/Administration/Mute/MuteService.cs +++ b/src/NadekoBot/Modules/Administration/Mute/MuteService.cs @@ -356,24 +356,24 @@ public class MuteService : INService public async Task TimedBan( IGuild guild, - IUser user, + ulong userId, TimeSpan after, string reason, int pruneDays) { - await guild.AddBanAsync(user.Id, pruneDays, reason); + await guild.AddBanAsync(userId, pruneDays, reason); await using (var uow = _db.GetDbContext()) { var config = uow.GuildConfigsForId(guild.Id, set => set.Include(x => x.UnbanTimer)); config.UnbanTimer.Add(new() { - UserId = user.Id, + UserId = userId, UnbanAt = DateTime.UtcNow + after }); // add teh unmute timer to the database - uow.SaveChanges(); + await uow.SaveChangesAsync(); } - StartUn_Timer(guild.Id, user.Id, after, TimerType.Ban); // start the timer + StartUn_Timer(guild.Id, userId, after, TimerType.Ban); // start the timer } public async Task TimedRole( diff --git a/src/NadekoBot/Modules/Administration/UserPunish/UserPunishCommands.cs b/src/NadekoBot/Modules/Administration/UserPunish/UserPunishCommands.cs index c83eede44..32c1e5db1 100644 --- a/src/NadekoBot/Modules/Administration/UserPunish/UserPunishCommands.cs +++ b/src/NadekoBot/Modules/Administration/UserPunish/UserPunishCommands.cs @@ -402,12 +402,21 @@ public partial class Administration [UserPerm(GuildPerm.BanMembers)] [BotPerm(GuildPerm.BanMembers)] [Priority(1)] - public async Task Ban(StoopidTime time, IUser user, [Leftover] string msg = null) + public Task Ban(StoopidTime time, IUser user, [Leftover] string msg = null) + => Ban(time, user.Id, msg); + + [Cmd] + [RequireContext(ContextType.Guild)] + [UserPerm(GuildPerm.BanMembers)] + [BotPerm(GuildPerm.BanMembers)] + [Priority(0)] + public async Task Ban(StoopidTime time, ulong userId, [Leftover] string msg = null) { if (time.Time > TimeSpan.FromDays(49)) return; - var guildUser = await ((DiscordSocketClient)Context.Client).Rest.GetGuildUserAsync(ctx.Guild.Id, user.Id); + var guildUser = await ((DiscordSocketClient)Context.Client).Rest.GetGuildUserAsync(ctx.Guild.Id, userId); + if (guildUser is not null && !await CheckRoleHierarchy(guildUser)) return; @@ -429,13 +438,14 @@ public partial class Administration } } + var user = await ctx.Client.GetUserAsync(userId); var banPrune = await _service.GetBanPruneAsync(ctx.Guild.Id) ?? 7; - await _mute.TimedBan(ctx.Guild, user, time.Time, (ctx.User + " | " + msg).TrimTo(512), banPrune); + await _mute.TimedBan(ctx.Guild, userId, time.Time, (ctx.User + " | " + msg).TrimTo(512), banPrune); var toSend = _eb.Create() .WithOkColor() .WithTitle("⛔️ " + GetText(strs.banned_user)) - .AddField(GetText(strs.username), user.ToString(), true) - .AddField("ID", user.Id.ToString(), true) + .AddField(GetText(strs.username), user?.ToString() ?? userId.ToString(), true) + .AddField("ID", userId.ToString(), true) .AddField(GetText(strs.duration), time.Time.Humanize(3, minUnit: TimeUnit.Minute, culture: Culture), true); diff --git a/src/NadekoBot/Modules/Administration/UserPunish/UserPunishService.cs b/src/NadekoBot/Modules/Administration/UserPunish/UserPunishService.cs index 1d6fb3273..7141905d9 100644 --- a/src/NadekoBot/Modules/Administration/UserPunish/UserPunishService.cs +++ b/src/NadekoBot/Modules/Administration/UserPunish/UserPunishService.cs @@ -157,7 +157,7 @@ public class UserPunishService : INService, IReadyExecutor if (minutes == 0) await guild.AddBanAsync(user, reason: reason, pruneDays: banPrune); else - await _mute.TimedBan(user.Guild, user, TimeSpan.FromMinutes(minutes), reason, banPrune); + await _mute.TimedBan(user.Guild, user.Id, TimeSpan.FromMinutes(minutes), reason, banPrune); break; case PunishmentAction.Softban: banPrune = await GetBanPruneAsync(user.GuildId) ?? 7;