elliebot/src/EllieBot/Modules/Administration/DangerousCommands/DangerousCommands.cs

163 lines
4.7 KiB
C#
Raw Normal View History

2024-09-21 00:13:18 +12:00
#nullable disable
using System.Globalization;
using CsvHelper;
using CsvHelper.Configuration;
using EllieBot.Modules.Gambling;
using EllieBot.Modules.Administration.Services;
using EllieBot.Modules.Xp;
namespace EllieBot.Modules.Administration;
public partial class Administration
{
[Group]
[OwnerOnly]
[NoPublicBot]
public partial class DangerousCommands : CleanupModuleBase
{
private readonly DangerousCommandsService _ds;
private readonly IGamblingCleanupService _gcs;
private readonly IXpCleanupService _xcs;
public DangerousCommands(
DangerousCommandsService ds,
IGamblingCleanupService gcs,
IXpCleanupService xcs)
{
_ds = ds;
_gcs = gcs;
_xcs = xcs;
}
[Cmd]
[OwnerOnly]
public Task SqlSelect([Leftover] string sql)
{
var result = _ds.SelectSql(sql);
return Response()
.Paginated()
.Items(result.Results)
.PageSize(20)
.Page((items, _) =>
{
if (!items.Any())
2024-11-28 01:06:01 +13:00
return CreateEmbed().WithErrorColor().WithFooter(sql).WithDescription("-");
2024-09-21 00:13:18 +12:00
2024-11-28 01:06:01 +13:00
return CreateEmbed()
2024-09-21 00:13:18 +12:00
.WithOkColor()
.WithFooter(sql)
.WithTitle(string.Join(" ║ ", result.ColumnNames))
.WithDescription(string.Join('\n', items.Select(x => string.Join(" ║ ", x))));
})
.SendAsync();
}
[Cmd]
[OwnerOnly]
public async Task SqlSelectCsv([Leftover] string sql)
{
var result = _ds.SelectSql(sql);
// create a file stream and write the data as csv
using var ms = new MemoryStream();
await using var sw = new StreamWriter(ms);
await using var csv = new CsvWriter(sw,
new CsvConfiguration(CultureInfo.InvariantCulture)
{
Delimiter = ","
});
foreach (var cn in result.ColumnNames)
{
csv.WriteField(cn);
}
await csv.NextRecordAsync();
foreach (var row in result.Results)
{
foreach (var field in row)
{
csv.WriteField(field);
}
await csv.NextRecordAsync();
}
await csv.FlushAsync();
ms.Position = 0;
// send the file
await ctx.Channel.SendFileAsync(ms, $"query_result_{DateTime.UtcNow.Ticks}.csv");
}
[Cmd]
[OwnerOnly]
public async Task SqlExec([Leftover] string sql)
{
try
{
2024-11-28 01:06:01 +13:00
var embed = CreateEmbed()
2024-09-21 00:13:18 +12:00
.WithTitle(GetText(strs.sql_confirm_exec))
.WithDescription(Format.Code(sql));
if (!await PromptUserConfirmAsync(embed))
return;
var res = await _ds.ExecuteSql(sql);
await Response().Confirm(res.ToString()).SendAsync();
}
catch (Exception ex)
{
await Response().Error(ex.ToString()).SendAsync();
}
}
[Cmd]
[OwnerOnly]
public async Task PurgeUser(ulong userId)
{
2024-11-28 01:06:01 +13:00
var embed = CreateEmbed()
2024-09-21 00:13:18 +12:00
.WithDescription(GetText(strs.purge_user_confirm(Format.Bold(userId.ToString()))));
if (!await PromptUserConfirmAsync(embed))
return;
await _ds.PurgeUserAsync(userId);
await ctx.OkAsync();
}
[Cmd]
[OwnerOnly]
public Task PurgeUser([Leftover] IUser user)
=> PurgeUser(user.Id);
[Cmd]
[OwnerOnly]
public Task DeleteXp()
=> ConfirmActionInternalAsync("Delete Xp", () => _xcs.DeleteXp());
[Cmd]
[OwnerOnly]
public Task DeleteWaifus()
=> ConfirmActionInternalAsync("Delete Waifus", () => _gcs.DeleteWaifus());
[Cmd]
[OwnerOnly]
public async Task DeleteWaifu(IUser user)
=> await DeleteWaifu(user.Id);
[Cmd]
[OwnerOnly]
public Task DeleteWaifu(ulong userId)
=> ConfirmActionInternalAsync($"Delete Waifu {userId}", () => _gcs.DeleteWaifu(userId));
[Cmd]
[OwnerOnly]
public Task DeleteCurrency()
=> ConfirmActionInternalAsync("Delete Currency", () => _gcs.DeleteCurrency());
}
}