Removed ban petitions

One mistake slipped through the cracks as I tested this:

It is not possible in any way for a user to DM a bot if they do not
share a guild. As of this moment, there is no workaround, and it
appears that simply opening a DM channel beforehand is not enough to
make it work.
In short: it doesn't work. It can't possibly work.

This feature has been removed. Alterations to the configuration
structure for ModTools will remain, as I planned to do this change
for other reasons not related to this feature anyway.
This commit is contained in:
Noikoio 2017-12-18 22:05:37 -08:00
parent c2cab91f31
commit dd8ef7ff3e
4 changed files with 4 additions and 166 deletions

View file

@ -20,9 +20,6 @@ namespace Noikoio.RegexBot.Module.ModTools.Commands
private readonly string _notifyMsg; private readonly string _notifyMsg;
const string DefaultMsg = "You have been {0} from $s for the following reason:\n$r"; const string DefaultMsg = "You have been {0} from $s for the following reason:\n$r";
const string DefaultMsgBanAppend = "\n\nIf the moderators have allowed it, you may petition your ban by" +
" submitting **one** message to the moderation team. To do so, reply to this message with" +
" `!petition [Your message here]`.";
// Configuration: // Configuration:
// "forcereason" - boolean; Force a reason to be given. Defaults to false. // "forcereason" - boolean; Force a reason to be given. Defaults to false.
@ -46,7 +43,6 @@ namespace Noikoio.RegexBot.Module.ModTools.Commands
{ {
// Message not specified - use default // Message not specified - use default
_notifyMsg = string.Format(DefaultMsg, mode == CommandMode.Ban ? "banned" : "kicked"); _notifyMsg = string.Format(DefaultMsg, mode == CommandMode.Ban ? "banned" : "kicked");
if (_mode == CommandMode.Ban) _notifyMsg += DefaultMsgBanAppend;
} }
else else
{ {
@ -131,9 +127,6 @@ namespace Noikoio.RegexBot.Module.ModTools.Commands
} }
else notifyfail = true; else notifyfail = true;
// Give target user ability to petition
if (_mode == CommandMode.Ban) Mt.AddPetition(g.Id, targetuid);
// Do the action // Do the action
try try
{ {

View file

@ -11,10 +11,8 @@ namespace Noikoio.RegexBot.Module.ModTools
/// </summary> /// </summary>
class ConfigItem class ConfigItem
{ {
private EntityName? _petitionReportCh;
private readonly ReadOnlyDictionary<string, CommandBase> _cmdInstances; private readonly ReadOnlyDictionary<string, CommandBase> _cmdInstances;
public EntityName? PetitionReportingChannel => _petitionReportCh;
public ReadOnlyDictionary<string, CommandBase> Commands => _cmdInstances; public ReadOnlyDictionary<string, CommandBase> Commands => _cmdInstances;
public ConfigItem(ModTools instance, JToken inconf) public ConfigItem(ModTools instance, JToken inconf)
@ -25,18 +23,6 @@ namespace Noikoio.RegexBot.Module.ModTools
} }
var config = (JObject)inconf; var config = (JObject)inconf;
// Ban petition reporting channel
var petitionstr = config["PetitionRelay"]?.Value<string>();
if (string.IsNullOrEmpty(petitionstr)) _petitionReportCh = null;
else if (petitionstr.Length > 1 && petitionstr[0] != '#')
{
// Not a channel.
throw new RuleImportException("PetitionRelay value must be set to a channel.");
}
else
{
_petitionReportCh = new EntityName(petitionstr.Substring(1), EntityType.Channel);
}
// Command instances // Command instances
var commands = new Dictionary<string, CommandBase>(StringComparer.OrdinalIgnoreCase); var commands = new Dictionary<string, CommandBase>(StringComparer.OrdinalIgnoreCase);
@ -62,14 +48,5 @@ namespace Noikoio.RegexBot.Module.ModTools
} }
_cmdInstances = new ReadOnlyDictionary<string, CommandBase>(commands); _cmdInstances = new ReadOnlyDictionary<string, CommandBase>(commands);
} }
public void UpdatePetitionChannel(ulong id)
{
if (!PetitionReportingChannel.HasValue) return;
if (PetitionReportingChannel.Value.Id.HasValue) return; // nothing to update
// For lack of a better option - create a new EntityName with ID already provided
_petitionReportCh = new EntityName($"{id}::{PetitionReportingChannel.Value.Name}", EntityType.Channel);
}
} }
} }

View file

@ -3,7 +3,6 @@ using Discord.WebSocket;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using Noikoio.RegexBot.ConfigItem; using Noikoio.RegexBot.ConfigItem;
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -27,8 +26,7 @@ namespace Noikoio.RegexBot.Module.ModTools
// Always ignore bots // Always ignore bots
if (arg.Author.IsBot) return; if (arg.Author.IsBot) return;
if (arg.Channel is IDMChannel) await PetitionRelayCheck(arg); if (arg.Channel is IGuildChannel) await CommandCheckInvoke(arg);
else if (arg.Channel is IGuildChannel) await CommandCheckInvoke(arg);
} }
[ConfigSection("ModTools")] [ConfigSection("ModTools")]
@ -40,8 +38,6 @@ namespace Noikoio.RegexBot.Module.ModTools
// Log results // Log results
if (conf.Commands.Count > 0) if (conf.Commands.Count > 0)
await Log(conf.Commands.Count + " command definition(s) loaded."); await Log(conf.Commands.Count + " command definition(s) loaded.");
if (conf.PetitionReportingChannel.HasValue)
await Log("Ban petitioning has been enabled.");
return conf; return conf;
} }
@ -83,133 +79,5 @@ namespace Noikoio.RegexBot.Module.ModTools
} }
} }
} }
#region Ban petitions
/// <summary>
/// List of available appeals. Key is user (for quick lookup). Value is guild (for quick config resolution).
/// TODO expiration?
/// </summary>
private Dictionary<ulong, ulong> _openPetitions = new Dictionary<ulong, ulong>();
public void AddPetition(ulong guild, ulong user)
{
// Do nothing if disabled
if (!GetConfig(guild).PetitionReportingChannel.HasValue) return;
lock (_openPetitions) _openPetitions[user] = guild;
}
private async Task PetitionRelayCheck(SocketMessage msg)
{
const string PetitionAccepted = "Your petition has been forwarded to the moderators for review.";
const string PetitionDenied = "You may not submit a ban petition.";
// It's possible the sender may still block messages sent to them,
// hence the empty catch blocks you'll see up ahead.
if (!msg.Content.StartsWith("!petition ", StringComparison.InvariantCultureIgnoreCase)) return;
// Input validation
string ptext = msg.Content.Substring(10);
if (string.IsNullOrWhiteSpace(ptext))
{
// Just ignore.
return;
}
if (ptext.Length > 1000)
{
// Enforce petition length limit.
try { await msg.Author.SendMessageAsync("Your petition message is too long. Try again with a shorter message."); }
catch (Discord.Net.HttpException) { }
return;
}
ulong targetGuild = 0;
lock (_openPetitions)
{
if (_openPetitions.TryGetValue(msg.Author.Id, out targetGuild))
{
_openPetitions.Remove(msg.Author.Id);
}
}
if (targetGuild == 0)
{
// Not in the list. Nothing to do.
try { await msg.Author.SendMessageAsync(PetitionDenied); }
catch (Discord.Net.HttpException) { }
return;
}
var gObj = Client.GetGuild(targetGuild);
if (gObj == null)
{
// Guild is missing. No longer in guild?
try { await msg.Author.SendMessageAsync(PetitionDenied); }
catch (Discord.Net.HttpException) { }
return;
}
// Get petition reporting target
var pcv = GetConfig(targetGuild).PetitionReportingChannel;
if (!pcv.HasValue) return; // No target. This should be logically impossible, but... just in case.
var rch = pcv.Value;
ISocketMessageChannel rchObj;
if (!rch.Id.HasValue)
{
rchObj = gObj.TextChannels
.Where(c => c.Name.Equals(rch.Name, StringComparison.InvariantCultureIgnoreCase))
.FirstOrDefault();
// Update value if found
if (rchObj != null)
{
GetConfig(targetGuild).UpdatePetitionChannel(rchObj.Id);
}
}
else
{
rchObj = gObj.GetChannel(rch.Id.Value) as ISocketMessageChannel;
}
if (rchObj == null)
{
// Channel not found.
await Log("Petition reporting channel could not be resolved.");
try { await msg.Author.SendMessageAsync(PetitionDenied); }
catch (Discord.Net.HttpException) { }
return;
}
// Ready to relay
try
{
await rchObj.SendMessageAsync("", embed: new EmbedBuilder()
{
Color = new Color(0x00FFD9),
Author = new EmbedAuthorBuilder()
{
Name = $"{msg.Author.ToString()} - Ban petition:",
IconUrl = msg.Author.GetAvatarUrl()
},
Description = ptext,
Timestamp = msg.Timestamp,
Footer = new EmbedFooterBuilder()
{
Text = "User ID: " + msg.Author.Id
}
});
}
catch (Discord.Net.HttpException ex)
{
await Log("Failed to relay petition message by " + msg.Author.ToString());
await Log(ex.Message);
// For the user's point of view, fail silently.
try { await msg.Author.SendMessageAsync(PetitionDenied); }
catch (Discord.Net.HttpException) { }
}
// Success. Notify user.
try { await msg.Author.SendMessageAsync(PetitionAccepted); }
catch (Discord.Net.HttpException) { }
}
#endregion
} }
} }

View file

@ -4,7 +4,7 @@
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework> <TargetFramework>netcoreapp2.0</TargetFramework>
<RootNamespace>Noikoio.RegexBot</RootNamespace> <RootNamespace>Noikoio.RegexBot</RootNamespace>
<AssemblyVersion>2.4.0.0</AssemblyVersion> <AssemblyVersion>2.4.1.0</AssemblyVersion>
<Description>Highly configurable Discord moderation bot</Description> <Description>Highly configurable Discord moderation bot</Description>
<Authors>Noikoio</Authors> <Authors>Noikoio</Authors>
<Company /> <Company />