1
Fork 0
mirror of https://gitlab.com/Kwoth/nadekobot.git synced 2024-10-03 12:33:14 +00:00

Added .banprune command which sets how many days worth of messages will be pruned when bot (soft)bans a person either through a command or another punishment feature. Also updated .next usage string.

This commit is contained in:
Kwoth 2022-08-31 17:47:19 +02:00
parent cd6fe46c2b
commit 59a1e56dad
17 changed files with 10258 additions and 13 deletions

View file

@ -5,4 +5,5 @@ public class BanTemplate : DbEntity
{ {
public ulong GuildId { get; set; } public ulong GuildId { get; set; }
public string Text { get; set; } public string Text { get; set; }
public int? PruneDays { get; set; }
} }

View file

@ -330,6 +330,10 @@ public abstract class NadekoContext : DbContext
#region BanTemplate #region BanTemplate
modelBuilder.Entity<BanTemplate>().HasIndex(x => x.GuildId).IsUnique(); modelBuilder.Entity<BanTemplate>().HasIndex(x => x.GuildId).IsUnique();
modelBuilder.Entity<BanTemplate>()
.Property(x => x.PruneDays)
.HasDefaultValue(null)
.IsRequired(false);
#endregion #endregion

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,25 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace NadekoBot.Migrations.Mysql
{
public partial class banprune : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "prunedays",
table: "bantemplates",
type: "int",
nullable: true);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "prunedays",
table: "bantemplates");
}
}
}

View file

@ -675,6 +675,10 @@ namespace NadekoBot.Migrations.Mysql
.HasColumnType("bigint unsigned") .HasColumnType("bigint unsigned")
.HasColumnName("guildid"); .HasColumnName("guildid");
b.Property<int?>("PruneDays")
.HasColumnType("int")
.HasColumnName("prunedays");
b.Property<string>("Text") b.Property<string>("Text")
.HasColumnType("longtext") .HasColumnType("longtext")
.HasColumnName("text"); .HasColumnName("text");

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,25 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace NadekoBot.Migrations.PostgreSql
{
public partial class banprune : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "prunedays",
table: "bantemplates",
type: "integer",
nullable: true);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "prunedays",
table: "bantemplates");
}
}
}

View file

@ -705,6 +705,10 @@ namespace NadekoBot.Migrations.PostgreSql
.HasColumnType("numeric(20,0)") .HasColumnType("numeric(20,0)")
.HasColumnName("guildid"); .HasColumnName("guildid");
b.Property<int?>("PruneDays")
.HasColumnType("integer")
.HasColumnName("prunedays");
b.Property<string>("Text") b.Property<string>("Text")
.HasColumnType("text") .HasColumnType("text")
.HasColumnName("text"); .HasColumnName("text");

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,25 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace NadekoBot.Migrations
{
public partial class banprune : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "PruneDays",
table: "BanTemplates",
type: "INTEGER",
nullable: true);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "PruneDays",
table: "BanTemplates");
}
}
}

View file

@ -529,6 +529,9 @@ namespace NadekoBot.Migrations
b.Property<ulong>("GuildId") b.Property<ulong>("GuildId")
.HasColumnType("INTEGER"); .HasColumnType("INTEGER");
b.Property<int?>("PruneDays")
.HasColumnType("INTEGER");
b.Property<string>("Text") b.Property<string>("Text")
.HasColumnType("TEXT"); .HasColumnType("TEXT");

View file

@ -358,9 +358,10 @@ public class MuteService : INService
IGuild guild, IGuild guild,
IUser user, IUser user,
TimeSpan after, TimeSpan after,
string reason) string reason,
int pruneDays)
{ {
await guild.AddBanAsync(user.Id, 0, reason); await guild.AddBanAsync(user.Id, pruneDays, reason);
await using (var uow = _db.GetDbContext()) await using (var uow = _db.GetDbContext())
{ {
var config = uow.GuildConfigsForId(guild.Id, set => set.Include(x => x.UnbanTimer)); var config = uow.GuildConfigsForId(guild.Id, set => set.Include(x => x.UnbanTimer));

View file

@ -429,7 +429,8 @@ public partial class Administration
} }
} }
await _mute.TimedBan(ctx.Guild, user, time.Time, (ctx.User + " | " + msg).TrimTo(512)); var banPrune = await _service.GetBanPruneAsync(ctx.Guild.Id) ?? 7;
await _mute.TimedBan(ctx.Guild, user, time.Time, (ctx.User + " | " + msg).TrimTo(512), banPrune);
var toSend = _eb.Create() var toSend = _eb.Create()
.WithOkColor() .WithOkColor()
.WithTitle("⛔️ " + GetText(strs.banned_user)) .WithTitle("⛔️ " + GetText(strs.banned_user))
@ -455,7 +456,8 @@ public partial class Administration
var user = await ((DiscordSocketClient)Context.Client).Rest.GetGuildUserAsync(ctx.Guild.Id, userId); var user = await ((DiscordSocketClient)Context.Client).Rest.GetGuildUserAsync(ctx.Guild.Id, userId);
if (user is null) if (user is null)
{ {
await ctx.Guild.AddBanAsync(userId, 7, (ctx.User + " | " + msg).TrimTo(512)); var banPrune = await _service.GetBanPruneAsync(ctx.Guild.Id) ?? 7;
await ctx.Guild.AddBanAsync(userId, banPrune, (ctx.User + " | " + msg).TrimTo(512));
await ctx.Channel.EmbedAsync(_eb.Create() await ctx.Channel.EmbedAsync(_eb.Create()
.WithOkColor() .WithOkColor()
@ -490,7 +492,8 @@ public partial class Administration
dmFailed = true; dmFailed = true;
} }
await ctx.Guild.AddBanAsync(user, 7, (ctx.User + " | " + msg).TrimTo(512)); var banPrune = await _service.GetBanPruneAsync(ctx.Guild.Id) ?? 7;
await ctx.Guild.AddBanAsync(user, banPrune, (ctx.User + " | " + msg).TrimTo(512));
var toSend = _eb.Create() var toSend = _eb.Create()
.WithOkColor() .WithOkColor()
@ -504,6 +507,26 @@ public partial class Administration
await ctx.Channel.EmbedAsync(toSend); await ctx.Channel.EmbedAsync(toSend);
} }
[Cmd]
[RequireContext(ContextType.Guild)]
[UserPerm(GuildPerm.BanMembers)]
[BotPerm(GuildPerm.BanMembers)]
public async Task BanPrune(int days)
{
if (days < 0 || days > 7)
{
await ReplyErrorLocalizedAsync(strs.invalid_input);
return;
}
await _service.SetBanPruneAsync(ctx.Guild.Id, days);
if (days == 0)
await ReplyConfirmLocalizedAsync(strs.ban_prune_disabled);
else
await ReplyConfirmLocalizedAsync(strs.ban_prune(days));
}
[Cmd] [Cmd]
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
[UserPerm(GuildPerm.BanMembers)] [UserPerm(GuildPerm.BanMembers)]
@ -655,7 +678,8 @@ public partial class Administration
dmFailed = true; dmFailed = true;
} }
await ctx.Guild.AddBanAsync(user, 7, ("Softban | " + ctx.User + " | " + msg).TrimTo(512)); var banPrune = await _service.GetBanPruneAsync(ctx.Guild.Id) ?? 7;
await ctx.Guild.AddBanAsync(user, banPrune, ("Softban | " + ctx.User + " | " + msg).TrimTo(512));
try { await ctx.Guild.RemoveBanAsync(user); } try { await ctx.Guild.RemoveBanAsync(user); }
catch { await ctx.Guild.RemoveBanAsync(user); } catch { await ctx.Guild.RemoveBanAsync(user); }
@ -822,11 +846,12 @@ public partial class Administration
var banningMessage = await ctx.Channel.EmbedAsync(toSend); var banningMessage = await ctx.Channel.EmbedAsync(toSend);
var banPrune = await _service.GetBanPruneAsync(ctx.Guild.Id) ?? 7;
foreach (var toBan in banning) foreach (var toBan in banning)
{ {
try try
{ {
await ctx.Guild.AddBanAsync(toBan.Id, 7, $"{ctx.User} | Massban"); await ctx.Guild.AddBanAsync(toBan.Id, banPrune, $"{ctx.User} | Massban");
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -865,10 +890,11 @@ public partial class Administration
.AddField(GetText(strs.invalid(missing)), missStr) .AddField(GetText(strs.invalid(missing)), missStr)
.WithPendingColor()); .WithPendingColor());
var banPrune = await _service.GetBanPruneAsync(ctx.Guild.Id) ?? 7;
//do the banning //do the banning
await Task.WhenAll(bans.Where(x => x.Id.HasValue) await Task.WhenAll(bans.Where(x => x.Id.HasValue)
.Select(x => ctx.Guild.AddBanAsync(x.Id.Value, .Select(x => ctx.Guild.AddBanAsync(x.Id.Value,
7, banPrune,
x.Reason, x.Reason,
new() new()
{ {

View file

@ -1,5 +1,6 @@
#nullable disable #nullable disable
using LinqToDB; using LinqToDB;
using LinqToDB.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using NadekoBot.Common.ModuleBehaviors; using NadekoBot.Common.ModuleBehaviors;
using NadekoBot.Common.TypeReaders.Models; using NadekoBot.Common.TypeReaders.Models;
@ -127,6 +128,7 @@ public class UserPunishService : INService, IReadyExecutor
if (!await CheckPermission(guild, p)) if (!await CheckPermission(guild, p))
return; return;
int banPrune;
switch (p) switch (p)
{ {
case PunishmentAction.Mute: case PunishmentAction.Mute:
@ -151,13 +153,15 @@ public class UserPunishService : INService, IReadyExecutor
await user.KickAsync(reason); await user.KickAsync(reason);
break; break;
case PunishmentAction.Ban: case PunishmentAction.Ban:
banPrune = await GetBanPruneAsync(user.GuildId) ?? 7;
if (minutes == 0) if (minutes == 0)
await guild.AddBanAsync(user, reason: reason, pruneDays: 7); await guild.AddBanAsync(user, reason: reason, pruneDays: banPrune);
else else
await _mute.TimedBan(user.Guild, user, TimeSpan.FromMinutes(minutes), reason); await _mute.TimedBan(user.Guild, user, TimeSpan.FromMinutes(minutes), reason, banPrune);
break; break;
case PunishmentAction.Softban: case PunishmentAction.Softban:
await guild.AddBanAsync(user, 7, $"Softban | {reason}"); banPrune = await GetBanPruneAsync(user.GuildId) ?? 7;
await guild.AddBanAsync(user, banPrune, $"Softban | {reason}");
try try
{ {
await guild.RemoveBanAsync(user); await guild.RemoveBanAsync(user);
@ -489,6 +493,37 @@ public class UserPunishService : INService, IReadyExecutor
uow.SaveChanges(); uow.SaveChanges();
} }
public async Task SetBanPruneAsync(ulong guildId, int? pruneDays)
{
await using var ctx = _db.GetDbContext();
await ctx.BanTemplates
.ToLinqToDBTable()
.InsertOrUpdateAsync(() => new()
{
GuildId = guildId,
Text = null,
DateAdded = DateTime.UtcNow,
PruneDays = pruneDays
},
old => new()
{
PruneDays = pruneDays
},
() => new()
{
GuildId = guildId
});
}
public async Task<int?> GetBanPruneAsync(ulong guildId)
{
await using var ctx = _db.GetDbContext();
return await ctx.BanTemplates
.Where(x => x.GuildId == guildId)
.Select(x => x.PruneDays)
.FirstOrDefaultAsyncLinqToDB();
}
public SmartText GetBanUserDmEmbed( public SmartText GetBanUserDmEmbed(
ICommandContext context, ICommandContext context,
IGuildUser target, IGuildUser target,

View file

@ -963,6 +963,8 @@ banmessagetest:
- banmsgtest - banmsgtest
banmsgreset: banmsgreset:
- banmsgreset - banmsgreset
banprune:
- banprune
timeout: timeout:
- timeout - timeout
wait: wait:

View file

@ -789,10 +789,9 @@ linux:
args: args:
- "Spyware Windows" - "Spyware Windows"
next: next:
desc: "Goes to the next song in the queue. You have to be in the same voice channel as the bot. You can skip multiple songs, but in that case songs will not be requeued if {0}rcs or {0}rpl is enabled." desc: "Goes to the next song in the queue. You have to be in the same voice channel as the bot"
args: args:
- "" - ""
- "5"
play: play:
desc: "If no parameters are specified, acts as `{0}next 1` command. If you specify a song number, it will jump to that song. If you specify a search query, acts as a `{0}q` command" desc: "If no parameters are specified, acts as `{0}next 1` command. If you specify a song number, it will jump to that song. If you specify a search query, acts as a `{0}q` command"
args: args:
@ -1681,6 +1680,13 @@ banmsgreset:
desc: "Resets ban message to default. If you want to completely disable ban messages, use `{0}banmsg -`" desc: "Resets ban message to default. If you want to completely disable ban messages, use `{0}banmsg -`"
args: args:
- "" - ""
banprune:
desc: |-
Sets how many days of messages will be deleted when a user is banned.
Only works if the user is banned via the .ban command or punishment.
Allowed values: 0 - 7
args:
- "3"
wait: wait:
desc: "Used only as a startup command. Waits a certain number of miliseconds before continuing the execution of the following startup commands." desc: "Used only as a startup command. Waits a certain number of miliseconds before continuing the execution of the following startup commands."
args: args:

View file

@ -28,6 +28,8 @@
"banmsg_default": "No ban message set. Default behavior will be used.", "banmsg_default": "No ban message set. Default behavior will be used.",
"banned_pl": "banned", "banned_pl": "banned",
"banned_user": "User Banned", "banned_user": "User Banned",
"ban_prune_disabled": "Banned user's messages will no longer be deleted.",
"ban_prune": "Bot will prune up to {0} day(s) worth of messages from banned user.",
"timeoutdm": "You have been timed out in {0} server.\nReason: {1}", "timeoutdm": "You have been timed out in {0} server.\nReason: {1}",
"timedout_user": "User Timed Out", "timedout_user": "User Timed Out",
"remove_roles_pl": "have had their roles removed", "remove_roles_pl": "have had their roles removed",