Discord: Allow replacing words/phrases sent to discord

This commit is contained in:
UnknownShadow200 2021-05-21 20:09:31 +10:00
parent 6eb8d6831c
commit e65b1888bf
4 changed files with 42 additions and 8 deletions

View File

@ -139,17 +139,17 @@ namespace MCGalaxy {
public static List<ChatToken> Custom = new List<ChatToken>();
static bool hookedCustom;
internal static void LoadCustom() {
Custom.Clear();
internal static void LoadCustom() {
TextFile tokensFile = TextFile.Files["Custom $s"];
tokensFile.EnsureExists();
if (!hookedCustom) {
hookedCustom = true;
tokensFile.OnTextChanged += LoadCustom;
}
}
string[] lines = tokensFile.GetText();
Custom.Clear();
LoadTokens(lines,
(key, value) => Custom.Add(new ChatToken(key, value, null)));
}

View File

@ -23,6 +23,7 @@ using MCGalaxy.Config;
using MCGalaxy.Events.GroupEvents;
using MCGalaxy.Events.PlayerEvents;
using MCGalaxy.Events.ServerEvents;
using MCGalaxy.Util;
namespace MCGalaxy.Modules.Relay.Discord {
@ -31,16 +32,23 @@ namespace MCGalaxy.Modules.Relay.Discord {
DiscordApiClient api;
DiscordWebsocket socket;
string botUserID;
Dictionary<string, bool> isDMChannel = new Dictionary<string, bool>();
List<string> filter_triggers = new List<string>();
List<string> filter_replacements = new List<string>();
public override string RelayName { get { return "Discord"; } }
public override bool Enabled { get { return Config.Enabled; } }
public override bool Connected { get { return socket != null && !disconnected; } }
public DiscordConfig Config;
public override void LoadControllers() {
Controllers = PlayerList.Load("ranks/Discord_Controllers.txt");
}
TextFile replacementsFile = new TextFile("text/discord/replacements.txt",
"// This file is used to replace words/phrases sent to discord",
"// Lines starting with // are ignored",
"// Lines should be formatted like this:",
"// example:http://example.org",
"// That would replace 'example' in messages sent with 'http://example.org'");
void TryReconnect() {
try {
@ -97,9 +105,11 @@ namespace MCGalaxy.Modules.Relay.Discord {
}
}
public override void ReloadConfig() {
Config.Load();
base.ReloadConfig();
LoadReplacements();
}
protected override void UpdateConfig() {
@ -109,6 +119,24 @@ namespace MCGalaxy.Modules.Relay.Discord {
LoadBannedCommands();
}
void LoadReplacements() {
replacementsFile.EnsureExists();
string[] lines = replacementsFile.GetText();
filter_triggers.Clear();
filter_replacements.Clear();
ChatTokens.LoadTokens(lines, (phrase, replacement) =>
{
filter_triggers.Add(phrase);
filter_replacements.Add(replacement);
});
}
public override void LoadControllers() {
Controllers = PlayerList.Load("text/discord/controllers.txt");
}
string GetNick(JsonObject data) {
if (!Config.UseNicks) return null;
@ -239,6 +267,11 @@ namespace MCGalaxy.Modules.Relay.Discord {
for (int i = 0; i < markdown_special.Length; i++) {
message = message.Replace(markdown_special[i], markdown_escaped[i]);
}
// allow uses to do things like replacing '+' with ':green_square:'
for (int i = 0; i < filter_triggers.Count; i++) {
message = message.Replace(filter_triggers[i], filter_replacements[i]);
}
return message;
}

View File

@ -148,7 +148,7 @@ namespace MCGalaxy.Modules.Relay {
protected abstract void UpdateConfig();
protected void LoadBannedCommands() {
BannedCommands = new List<string>() { "IRCBot", "DiscordBot", "OpRules", "IRCControllers" };
BannedCommands = new List<string>() { "IRCBot", "DiscordBot", "OpRules", "IRCControllers", "DiscordControllers" };
if (!File.Exists("text/irccmdblacklist.txt")) {
File.WriteAllLines("text/irccmdblacklist.txt", new string[] {

View File

@ -149,6 +149,7 @@ namespace MCGalaxy {
EnsureDirectoryExists("blockdefs");
EnsureDirectoryExists(IScripting.DllDir);
EnsureDirectoryExists(ICompiler.SourceDir);
EnsureDirectoryExists("text/discord"); // TODO move to discord plugin
}
static void EnsureDirectoryExists(string dir) {