mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-10 09:18:27 -04:00
Added -a / --after <messageid> option to .prune, converted the argument to proper options object
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
#nullable disable
|
#nullable disable
|
||||||
|
using CommandLine;
|
||||||
using NadekoBot.Modules.Administration.Services;
|
using NadekoBot.Modules.Administration.Services;
|
||||||
|
|
||||||
namespace NadekoBot.Modules.Administration;
|
namespace NadekoBot.Modules.Administration;
|
||||||
@@ -10,17 +11,34 @@ public partial class Administration
|
|||||||
{
|
{
|
||||||
private static readonly TimeSpan _twoWeeks = TimeSpan.FromDays(14);
|
private static readonly TimeSpan _twoWeeks = TimeSpan.FromDays(14);
|
||||||
|
|
||||||
//delets her own messages, no perm required
|
public sealed class PruneOptions : INadekoCommandOptions
|
||||||
|
{
|
||||||
|
[Option(shortName: 's', longName: "safe", Default = false, HelpText = "Whether pinned messages should be deleted.", Required = false)]
|
||||||
|
public bool Safe { get; set; }
|
||||||
|
|
||||||
|
[Option(shortName: 'a', longName: "after", Default = null, HelpText = "Prune only messages after the specified message ID.", Required = false)]
|
||||||
|
public ulong? After { get; set; }
|
||||||
|
|
||||||
|
public void NormalizeOptions()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//deletes her own messages, no perm required
|
||||||
[Cmd]
|
[Cmd]
|
||||||
[RequireContext(ContextType.Guild)]
|
[RequireContext(ContextType.Guild)]
|
||||||
public async Task Prune(string parameter = null)
|
[NadekoOptions(typeof(PruneOptions))]
|
||||||
|
public async Task Prune(params string[] args)
|
||||||
{
|
{
|
||||||
|
var (opts, _) = OptionsParser.ParseFrom<PruneOptions>(new PruneOptions(), args);
|
||||||
|
|
||||||
var user = await ctx.Guild.GetCurrentUserAsync();
|
var user = await ctx.Guild.GetCurrentUserAsync();
|
||||||
|
|
||||||
if (parameter is "-s" or "--safe")
|
if (opts.Safe)
|
||||||
await _service.PruneWhere((ITextChannel)ctx.Channel, 100, x => x.Author.Id == user.Id && !x.IsPinned);
|
await _service.PruneWhere((ITextChannel)ctx.Channel, 100, x => x.Author.Id == user.Id && !x.IsPinned, opts.After);
|
||||||
else
|
else
|
||||||
await _service.PruneWhere((ITextChannel)ctx.Channel, 100, x => x.Author.Id == user.Id);
|
await _service.PruneWhere((ITextChannel)ctx.Channel, 100, x => x.Author.Id == user.Id, opts.After);
|
||||||
|
|
||||||
ctx.Message.DeleteAfter(3);
|
ctx.Message.DeleteAfter(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,19 +47,22 @@ public partial class Administration
|
|||||||
[RequireContext(ContextType.Guild)]
|
[RequireContext(ContextType.Guild)]
|
||||||
[UserPerm(ChannelPerm.ManageMessages)]
|
[UserPerm(ChannelPerm.ManageMessages)]
|
||||||
[BotPerm(ChannelPerm.ManageMessages)]
|
[BotPerm(ChannelPerm.ManageMessages)]
|
||||||
|
[NadekoOptions(typeof(PruneOptions))]
|
||||||
[Priority(1)]
|
[Priority(1)]
|
||||||
public async Task Prune(int count, string parameter = null)
|
public async Task Prune(int count, params string[] args)
|
||||||
{
|
{
|
||||||
count++;
|
count++;
|
||||||
if (count < 1)
|
if (count < 1)
|
||||||
return;
|
return;
|
||||||
if (count > 1000)
|
if (count > 1000)
|
||||||
count = 1000;
|
count = 1000;
|
||||||
|
|
||||||
|
var (opts, _) = OptionsParser.ParseFrom<PruneOptions>(new PruneOptions(), args);
|
||||||
|
|
||||||
if (parameter is "-s" or "--safe")
|
if (opts.Safe)
|
||||||
await _service.PruneWhere((ITextChannel)ctx.Channel, count, x => !x.IsPinned);
|
await _service.PruneWhere((ITextChannel)ctx.Channel, count, x => !x.IsPinned, opts.After);
|
||||||
else
|
else
|
||||||
await _service.PruneWhere((ITextChannel)ctx.Channel, count, _ => true);
|
await _service.PruneWhere((ITextChannel)ctx.Channel, count, _ => true, opts.After);
|
||||||
}
|
}
|
||||||
|
|
||||||
//prune @user [x]
|
//prune @user [x]
|
||||||
@@ -49,17 +70,19 @@ public partial class Administration
|
|||||||
[RequireContext(ContextType.Guild)]
|
[RequireContext(ContextType.Guild)]
|
||||||
[UserPerm(ChannelPerm.ManageMessages)]
|
[UserPerm(ChannelPerm.ManageMessages)]
|
||||||
[BotPerm(ChannelPerm.ManageMessages)]
|
[BotPerm(ChannelPerm.ManageMessages)]
|
||||||
|
[NadekoOptions(typeof(PruneOptions))]
|
||||||
[Priority(0)]
|
[Priority(0)]
|
||||||
public Task Prune(IGuildUser user, int count = 100, string parameter = null)
|
public Task Prune(IGuildUser user, int count = 100, string args = null)
|
||||||
=> Prune(user.Id, count, parameter);
|
=> Prune(user.Id, count, args);
|
||||||
|
|
||||||
//prune userid [x]
|
//prune userid [x]
|
||||||
[Cmd]
|
[Cmd]
|
||||||
[RequireContext(ContextType.Guild)]
|
[RequireContext(ContextType.Guild)]
|
||||||
[UserPerm(ChannelPerm.ManageMessages)]
|
[UserPerm(ChannelPerm.ManageMessages)]
|
||||||
[BotPerm(ChannelPerm.ManageMessages)]
|
[BotPerm(ChannelPerm.ManageMessages)]
|
||||||
|
[NadekoOptions(typeof(PruneOptions))]
|
||||||
[Priority(0)]
|
[Priority(0)]
|
||||||
public async Task Prune(ulong userId, int count = 100, string parameter = null)
|
public async Task Prune(ulong userId, int count = 100, params string[] args)
|
||||||
{
|
{
|
||||||
if (userId == ctx.User.Id)
|
if (userId == ctx.User.Id)
|
||||||
count++;
|
count++;
|
||||||
@@ -70,17 +93,21 @@ public partial class Administration
|
|||||||
if (count > 1000)
|
if (count > 1000)
|
||||||
count = 1000;
|
count = 1000;
|
||||||
|
|
||||||
if (parameter is "-s" or "--safe")
|
var (opts, _) = OptionsParser.ParseFrom<PruneOptions>(new PruneOptions(), args);
|
||||||
|
|
||||||
|
if (opts.Safe)
|
||||||
{
|
{
|
||||||
await _service.PruneWhere((ITextChannel)ctx.Channel,
|
await _service.PruneWhere((ITextChannel)ctx.Channel,
|
||||||
count,
|
count,
|
||||||
m => m.Author.Id == userId && DateTime.UtcNow - m.CreatedAt < _twoWeeks && !m.IsPinned);
|
m => m.Author.Id == userId && DateTime.UtcNow - m.CreatedAt < _twoWeeks && !m.IsPinned,
|
||||||
|
opts.After);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
await _service.PruneWhere((ITextChannel)ctx.Channel,
|
await _service.PruneWhere((ITextChannel)ctx.Channel,
|
||||||
count,
|
count,
|
||||||
m => m.Author.Id == userId && DateTime.UtcNow - m.CreatedAt < _twoWeeks);
|
m => m.Author.Id == userId && DateTime.UtcNow - m.CreatedAt < _twoWeeks,
|
||||||
|
opts.After);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,6 +1,4 @@
|
|||||||
#nullable disable
|
#nullable disable
|
||||||
using Nadeko.Common;
|
|
||||||
|
|
||||||
namespace NadekoBot.Modules.Administration.Services;
|
namespace NadekoBot.Modules.Administration.Services;
|
||||||
|
|
||||||
public class PruneService : INService
|
public class PruneService : INService
|
||||||
@@ -13,7 +11,7 @@ public class PruneService : INService
|
|||||||
public PruneService(ILogCommandService logService)
|
public PruneService(ILogCommandService logService)
|
||||||
=> _logService = logService;
|
=> _logService = logService;
|
||||||
|
|
||||||
public async Task PruneWhere(ITextChannel channel, int amount, Func<IMessage, bool> predicate)
|
public async Task PruneWhere(ITextChannel channel, int amount, Func<IMessage, bool> predicate, ulong? after = null)
|
||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(channel, nameof(channel));
|
ArgumentNullException.ThrowIfNull(channel, nameof(channel));
|
||||||
|
|
||||||
@@ -28,7 +26,14 @@ public class PruneService : INService
|
|||||||
var now = DateTime.UtcNow;
|
var now = DateTime.UtcNow;
|
||||||
IMessage[] msgs;
|
IMessage[] msgs;
|
||||||
IMessage lastMessage = null;
|
IMessage lastMessage = null;
|
||||||
msgs = (await channel.GetMessagesAsync(50).FlattenAsync()).Where(predicate).Take(amount).ToArray();
|
var dled = await channel.GetMessagesAsync(50).FlattenAsync();
|
||||||
|
|
||||||
|
msgs = dled
|
||||||
|
.Where(predicate)
|
||||||
|
.Where(x => after is ulong a ? x.Id > a : true)
|
||||||
|
.Take(amount)
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
while (amount > 0 && msgs.Any())
|
while (amount > 0 && msgs.Any())
|
||||||
{
|
{
|
||||||
lastMessage = msgs[^1];
|
lastMessage = msgs[^1];
|
||||||
@@ -62,10 +67,13 @@ public class PruneService : INService
|
|||||||
amount -= 50;
|
amount -= 50;
|
||||||
if (amount > 0)
|
if (amount > 0)
|
||||||
{
|
{
|
||||||
msgs = (await channel.GetMessagesAsync(lastMessage, Direction.Before, 50).FlattenAsync())
|
dled = await channel.GetMessagesAsync(lastMessage, Direction.Before, 50).FlattenAsync();
|
||||||
.Where(predicate)
|
|
||||||
.Take(amount)
|
msgs = dled
|
||||||
.ToArray();
|
.Where(predicate)
|
||||||
|
.Where(x => after is ulong a ? x.Id > a : true)
|
||||||
|
.Take(amount)
|
||||||
|
.ToArray();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user