Add channel blacklists for logging and proxying
Necessary database migrations for this commit: alter table servers add column log_blacklist bigint[] not null default array[]::bigint[]; alter table servers add column blacklist bigint[] not null default array[]::bigint[];
This commit is contained in:
@@ -45,7 +45,11 @@ namespace PluralKit.Bot.Commands
|
||||
public static Command Export = new Command("export", "export", "Exports system information to a data file");
|
||||
public static Command Help = new Command("help", "help", "Shows help information about PluralKit");
|
||||
public static Command Message = new Command("message", "message <id|link>", "Looks up a proxied message");
|
||||
public static Command Log = new Command("log", "log <channel>", "Designates a channel to post proxied messages to");
|
||||
public static Command LogChannel = new Command("log channel", "log channel <channel>", "Designates a channel to post proxied messages to");
|
||||
public static Command LogEnable = new Command("log enable", "log enable all|<channel> [channel 2] [channel 3...]", "Enables message logging in certain channels");
|
||||
public static Command LogDisable = new Command("log disable", "log disable all|<channel> [channel 2] [channel 3...]", "Disables message logging in certain channels");
|
||||
public static Command BlacklistAdd = new Command("blacklist add", "blacklist add all|<channel> [channel 2] [channel 3...]", "Adds certain channels to the proxy blacklist");
|
||||
public static Command BlacklistRemove = new Command("blacklist remove", "blacklist remove all|<channel> [channel 2] [channel 3...]", "Removes certain channels from the proxy blacklist");
|
||||
public static Command Invite = new Command("invite", "invite", "Gets a link to invite PluralKit to other servers");
|
||||
public static Command PermCheck = new Command("permcheck", "permcheck <guild>", "Checks whether a server's permission setup is correct");
|
||||
|
||||
@@ -60,6 +64,8 @@ namespace PluralKit.Bot.Commands
|
||||
};
|
||||
|
||||
public static Command[] SwitchCommands = {Switch, SwitchOut, SwitchMove, SwitchDelete};
|
||||
|
||||
public static Command[] LogCommands = {LogChannel, LogEnable, LogDisable};
|
||||
|
||||
private IDiscordClient _client;
|
||||
|
||||
@@ -100,7 +106,19 @@ namespace PluralKit.Bot.Commands
|
||||
if (ctx.Match("message", "msg"))
|
||||
return ctx.Execute<ModCommands>(Message, m => m.GetMessage(ctx));
|
||||
if (ctx.Match("log"))
|
||||
return ctx.Execute<ModCommands>(Log, m => m.SetLogChannel(ctx));
|
||||
if (ctx.Match("channel"))
|
||||
return ctx.Execute<ModCommands>(LogChannel, m => m.SetLogChannel(ctx));
|
||||
else if (ctx.Match("enable", "on"))
|
||||
return ctx.Execute<ModCommands>(LogEnable, m => m.SetLogEnabled(ctx, true));
|
||||
else if (ctx.Match("disable", "off"))
|
||||
return ctx.Execute<ModCommands>(LogDisable, m => m.SetLogEnabled(ctx, false));
|
||||
else return PrintCommandExpectedError(ctx, LogCommands);
|
||||
if (ctx.Match("blacklist", "bl"))
|
||||
if (ctx.Match("enable", "on", "add", "deny"))
|
||||
return ctx.Execute<ModCommands>(BlacklistAdd, m => m.SetBlacklisted(ctx, true));
|
||||
else if (ctx.Match("disable", "off", "remove", "allow"))
|
||||
return ctx.Execute<ModCommands>(BlacklistRemove, m => m.SetBlacklisted(ctx, false));
|
||||
else return PrintCommandExpectedError(ctx, BlacklistAdd, BlacklistRemove);
|
||||
if (ctx.Match("invite")) return ctx.Execute<MiscCommands>(Invite, m => m.Invite(ctx));
|
||||
if (ctx.Match("mn")) return ctx.Execute<MiscCommands>(null, m => m.Mn(ctx));
|
||||
if (ctx.Match("fire")) return ctx.Execute<MiscCommands>(null, m => m.Fire(ctx));
|
||||
|
@@ -1,3 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using Discord;
|
||||
@@ -27,8 +29,9 @@ namespace PluralKit.Bot.Commands
|
||||
ITextChannel channel = null;
|
||||
if (ctx.HasNext())
|
||||
channel = ctx.MatchChannel() ?? throw new PKSyntaxError("You must pass a #channel to set.");
|
||||
if (channel != null && channel.GuildId != ctx.Guild.Id) throw new PKError("That channel is not in this server!");
|
||||
|
||||
var cfg = await _data.GetGuildConfig(ctx.Guild.Id);
|
||||
var cfg = await _data.GetOrCreateGuildConfig(ctx.Guild.Id);
|
||||
cfg.LogChannel = channel?.Id;
|
||||
await _data.SaveGuildConfig(cfg);
|
||||
|
||||
@@ -37,6 +40,57 @@ namespace PluralKit.Bot.Commands
|
||||
else
|
||||
await ctx.Reply($"{Emojis.Success} Proxy logging channel cleared.");
|
||||
}
|
||||
|
||||
public async Task SetLogEnabled(Context ctx, bool enable)
|
||||
{
|
||||
ctx.CheckGuildContext().CheckAuthorPermission(GuildPermission.ManageGuild, "Manage Server");
|
||||
|
||||
var affectedChannels = new List<ITextChannel>();
|
||||
if (ctx.Match("all"))
|
||||
affectedChannels = (await ctx.Guild.GetChannelsAsync()).OfType<ITextChannel>().ToList();
|
||||
else if (!ctx.HasNext()) throw new PKSyntaxError("You must pass one or more #channels.");
|
||||
else while (ctx.HasNext())
|
||||
{
|
||||
if (!(ctx.MatchChannel() is ITextChannel channel))
|
||||
throw new PKSyntaxError($"Channel \"{ctx.PopArgument().SanitizeMentions()}\" not found.");
|
||||
if (channel.GuildId != ctx.Guild.Id) throw new PKError($"Channel {ctx.Guild.Id} is not in this server.");
|
||||
affectedChannels.Add(channel);
|
||||
}
|
||||
|
||||
var guildCfg = await _data.GetOrCreateGuildConfig(ctx.Guild.Id);
|
||||
if (enable) guildCfg.LogBlacklist.ExceptWith(affectedChannels.Select(c => c.Id));
|
||||
else guildCfg.LogBlacklist.UnionWith(affectedChannels.Select(c => c.Id));
|
||||
|
||||
await _data.SaveGuildConfig(guildCfg);
|
||||
await ctx.Reply(
|
||||
$"{Emojis.Success} Message logging for the given channels {(enable ? "enabled" : "disabled")}." +
|
||||
(guildCfg.LogChannel == null ? $"\n{Emojis.Warn} Please note that no logging channel is set, so there is nowhere to log messages to. You can set a logging channel using `pk;log channel #your-log-channel`." : ""));
|
||||
}
|
||||
|
||||
public async Task SetBlacklisted(Context ctx, bool onBlacklist)
|
||||
{
|
||||
ctx.CheckGuildContext().CheckAuthorPermission(GuildPermission.ManageGuild, "Manage Server");
|
||||
|
||||
var affectedChannels = new List<ITextChannel>();
|
||||
if (ctx.Match("all"))
|
||||
affectedChannels = (await ctx.Guild.GetChannelsAsync()).OfType<ITextChannel>().ToList();
|
||||
else if (!ctx.HasNext()) throw new PKSyntaxError("You must pass one or more #channels.");
|
||||
else while (ctx.HasNext())
|
||||
{
|
||||
if (!(ctx.MatchChannel() is ITextChannel channel))
|
||||
throw new PKSyntaxError($"Channel \"{ctx.PopArgument().SanitizeMentions()}\" not found.");
|
||||
if (channel.GuildId != ctx.Guild.Id) throw new PKError($"Channel {ctx.Guild.Id} is not in this server.");
|
||||
affectedChannels.Add(channel);
|
||||
}
|
||||
|
||||
var guildCfg = await _data.GetOrCreateGuildConfig(ctx.Guild.Id);
|
||||
if (onBlacklist) guildCfg.Blacklist.UnionWith(affectedChannels.Select(c => c.Id));
|
||||
else guildCfg.Blacklist.ExceptWith(affectedChannels.Select(c => c.Id));
|
||||
|
||||
await _data.SaveGuildConfig(guildCfg);
|
||||
await ctx.Reply($"{Emojis.Success} Channels {(onBlacklist ? "added to" : "removed from")} the proxy blacklist.");
|
||||
|
||||
}
|
||||
|
||||
public async Task GetMessage(Context ctx)
|
||||
{
|
||||
|
Reference in New Issue
Block a user