mirror of
https://github.com/RPCS3/discord-bot.git
synced 2026-01-31 01:25:22 +01:00
RPCS3 Compatibility Bot reimplemented in C# for .NET Core Current status of this PR: * tested and targeted for .NET Core 2.1 * all functionality is either on par or improved compared to the python version * compatibility with current bot.db should be preserved in all upgrade scenarios * some bot management commands were changed (now under !sudo bot) * standard help generator for the new discord client is ... different; compatibility with old format could be restored through custom formatter if needed * everything has been split in more loosely tied components for easier extensibility and maintenance * log parsing has been rewritten and should work ~2x as fast
72 lines
3.1 KiB
C#
72 lines
3.1 KiB
C#
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using CompatBot.Attributes;
|
|
using CompatBot.Database;
|
|
using CompatBot.Providers;
|
|
using CompatBot.Utils;
|
|
using DSharpPlus.CommandsNext;
|
|
using DSharpPlus.CommandsNext.Attributes;
|
|
using DSharpPlus.Entities;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace CompatBot.Commands
|
|
{
|
|
[Group("piracy"), RequiresBotModRole, RequiresDm]
|
|
[Description("Used to manage piracy filters **in DM**")]
|
|
internal sealed class Antipiracy: BaseCommandModule
|
|
{
|
|
[Command("list"), Aliases("show")]
|
|
[Description("Lists all filters")]
|
|
public async Task List(CommandContext ctx)
|
|
{
|
|
var typingTask = ctx.TriggerTypingAsync();
|
|
var result = new StringBuilder("```")
|
|
.AppendLine("ID | Trigger")
|
|
.AppendLine("-----------------------------");
|
|
foreach (var item in await BotDb.Instance.Piracystring.ToListAsync().ConfigureAwait(false))
|
|
result.AppendLine($"{item.Id:0000} | {item.String}");
|
|
await ctx.SendAutosplitMessageAsync(result.Append("```")).ConfigureAwait(false);
|
|
await typingTask;
|
|
}
|
|
|
|
[Command("add")]
|
|
[Description("Adds a new piracy filter trigger")]
|
|
public async Task Add(CommandContext ctx, [RemainingText, Description("A plain string to match")] string trigger)
|
|
{
|
|
var typingTask = ctx.TriggerTypingAsync();
|
|
var wasSuccessful = await PiracyStringProvider.AddAsync(trigger).ConfigureAwait(false);
|
|
(DiscordEmoji reaction, string msg) result = wasSuccessful
|
|
? (Config.Reactions.Success, "New trigger successfully saved!")
|
|
: (Config.Reactions.Failure, "Trigger already defined.");
|
|
await Task.WhenAll(
|
|
ctx.RespondAsync(result.msg),
|
|
ctx.Message.CreateReactionAsync(result.reaction),
|
|
typingTask
|
|
).ConfigureAwait(false);
|
|
if (wasSuccessful)
|
|
await List(ctx).ConfigureAwait(false);
|
|
}
|
|
|
|
[Command("remove"), Aliases("delete", "del")]
|
|
[Description("Removes a piracy filter trigger")]
|
|
public async Task Remove(CommandContext ctx, [Description("Filter ids to remove separated with spaces")] params int[] ids)
|
|
{
|
|
var typingTask = ctx.TriggerTypingAsync();
|
|
(DiscordEmoji reaction, string msg) result = (Config.Reactions.Success, $"Trigger{(ids.Length == 1 ? "" : "s")} successfully removed!");
|
|
var failedIds = new List<int>();
|
|
foreach (var id in ids)
|
|
if (!await PiracyStringProvider.RemoveAsync(id).ConfigureAwait(false))
|
|
failedIds.Add(id);
|
|
if (failedIds.Count > 0)
|
|
result = (Config.Reactions.Failure, "Some ids couldn't be removed: " + string.Join(", ", failedIds));
|
|
await Task.WhenAll(
|
|
ctx.RespondAsync(result.msg),
|
|
ctx.Message.CreateReactionAsync(result.reaction),
|
|
typingTask
|
|
).ConfigureAwait(false);
|
|
await List(ctx).ConfigureAwait(false);
|
|
}
|
|
}
|
|
}
|