updates better relay storage

This commit is contained in:
Madpeterz 2021-04-05 20:56:09 +01:00
parent 810c9fb3e7
commit 958aceaa6b
3 changed files with 75 additions and 79 deletions

View file

@ -40,53 +40,14 @@ namespace BetterSecondBot.BetterRelayService
attach_events(); attach_events();
} }
protected void ApplyRelayConfig(string raw) protected void ApplyRelayConfig(string sourcename,string sourcevalue,string targetname,string targetvalue,bool jsonEncoded)
{
string[] bits = raw.Split(",");
bool have_source_type = false;
bool have_source_filter = false;
bool have_target_type = false;
bool have_target_filter = false;
if(bits.Length >= 4)
{ {
relay_config relay = new relay_config(); relay_config relay = new relay_config();
foreach (string a in bits) relay.encode_as_json = jsonEncoded;
{ relay.sourcename = sourcename;
string[] subbits = a.Split("::"); relay.sourcevalue = sourcevalue;
if(subbits.Length == 2) relay.targetname = targetname;
{ relay.targetvalue = targetvalue;
if(subbits[0] == "source-type")
{
have_source_type = true;
relay.sourcename = subbits[1];
}
else if (subbits[0] == "source-filter")
{
have_source_filter = true;
relay.sourcevalue = subbits[1];
}
else if (subbits[0] == "target-type")
{
have_target_type = true;
relay.targetname = subbits[1];
}
else if (subbits[0] == "target-config")
{
have_target_filter = true;
relay.targetvalue = subbits[1];
}
else if(subbits[0] == "encode-as-json")
{
if(bool.TryParse(subbits[1],out bool use_json_encode) == true)
{
relay.encode_as_json = use_json_encode;
}
}
}
}
List<bool> tests = new List<bool>() { have_source_type, have_source_filter, have_target_type, have_target_filter };
if(tests.Contains(false) == false)
{
if (relay.sourcename == "localchat") if (relay.sourcename == "localchat")
{ {
LocalchatRelay.Add(relay); LocalchatRelay.Add(relay);
@ -108,23 +69,35 @@ namespace BetterSecondBot.BetterRelayService
DiscordRelay.Add(relay); DiscordRelay.Add(relay);
} }
} }
}
}
protected void loadRelayConfigFromENV() protected void loadRelayConfigFromENV()
{ {
int loop = 1; int loop = 1;
bool found = true; bool found = true;
string[] needBits = new string[]{ "asJson", "sourceType", "sourceFilter", "targetType", "targetConfig" };
while (found == true) while (found == true)
{ {
if (helpers.notempty(Environment.GetEnvironmentVariable("CustomRelay_" + loop.ToString())) == true) bool configfound = true;
Dictionary<string, string> config = new Dictionary<string, string>();
foreach(string a in needBits)
{ {
ApplyRelayConfig(Environment.GetEnvironmentVariable("CustomRelay_" + loop.ToString())); if (helpers.notempty(Environment.GetEnvironmentVariable("CustomRelay_" + loop.ToString()+"_"+a)) == false)
{
configfound = false;
break;
} }
else else
{ {
found = false; config.Add(a, "CustomRelay_" + loop.ToString() + "_" + a);
} }
}
if(configfound == false)
{
continue;
}
bool asJson = false;
bool.TryParse(config["asJson"], out asJson);
ApplyRelayConfig(config["sourceType"], config["sourceFilter"], config["targetType"], config["targetConfig"], asJson);
loop++; loop++;
} }
} }
@ -133,9 +106,10 @@ namespace BetterSecondBot.BetterRelayService
protected void loadRelayConfigFromFile() protected void loadRelayConfigFromFile()
{ {
JsonCustomRelays LoadedRelays = new JsonCustomRelays JsonCustomRelays Demo = new JsonCustomRelays() { sourceFilter = "all", sourceType = "localchat", targetType = "groupchat", targetConfig = UUID.Zero.ToString() };
JsonCustomRelaysSet LoadedRelays = new JsonCustomRelaysSet
{ {
CustomRelays = new [] { "source-type:discord,source-filter:123451231235@12351312321,target-type:localchat,target-config:0" } Entrys = new JsonCustomRelays[] { Demo }
}; };
string targetfile = "customrelays.json"; string targetfile = "customrelays.json";
SimpleIO io = new SimpleIO(); SimpleIO io = new SimpleIO();
@ -154,10 +128,10 @@ namespace BetterSecondBot.BetterRelayService
{ {
try try
{ {
LoadedRelays = JsonConvert.DeserializeObject<JsonCustomRelays>(json); LoadedRelays = JsonConvert.DeserializeObject<JsonCustomRelaysSet>(json);
foreach (string loaded in LoadedRelays.CustomRelays) foreach (JsonCustomRelays loaded in LoadedRelays.Entrys)
{ {
ApplyRelayConfig(loaded); ApplyRelayConfig(loaded.sourceType, loaded.sourceFilter, loaded.targetType, loaded.targetConfig, loaded.encodeJson);
} }
} }
catch catch
@ -210,7 +184,9 @@ namespace BetterSecondBot.BetterRelayService
{ {
return Task.CompletedTask; return Task.CompletedTask;
} }
string message_no_name = "[relay]" + message; string message_no_name = "[relay]" + message;
string message_no_addon = message;
message = "[relay] " + sourcetype + " # " + name + ": " + message; message = "[relay] " + sourcetype + " # " + name + ": " + message;
@ -231,14 +207,25 @@ namespace BetterSecondBot.BetterRelayService
if (((cfg.sourcevalue == "all") && (sourcetype != "discordchat")) || (cfg.sourcevalue == filtervalue)) if (((cfg.sourcevalue == "all") && (sourcetype != "discordchat")) || (cfg.sourcevalue == filtervalue))
{ {
if (cfg.targetname == "discord") if(cfg.targetname == "discordTTS")
{ {
string[] cfga = cfg.targetvalue.Split("@"); string[] cfga = cfg.targetvalue.Split("@");
if (cfga.Length == 2) if (cfga.Length == 2)
{ {
if ((cfga[0] + "@" + cfga[1]) != filtervalue) if ((cfga[0] + "@" + cfga[1]) != filtervalue)
{ {
await controler.getBot().SendMessageToDiscord(cfga[0], cfga[1], sendmessage, false); await controler.getBot().SendMessageToDiscord(cfga[0], cfga[1], message_no_addon, true);
}
}
}
else if (cfg.targetname == "discord")
{
string[] cfga = cfg.targetvalue.Split("@");
if (cfga.Length == 2)
{
if ((cfga[0] + "@" + cfga[1]) != filtervalue)
{
await controler.getBot().SendMessageToDiscord(cfga[0], cfga[1], message_no_addon, false);
} }
} }
} }

View file

@ -69,7 +69,7 @@ namespace BetterSecondBotShared.IO
} }
} }
public void WriteJsonRelays(JsonCustomRelays relays, string targetfile) public void WriteJsonRelays(JsonCustomRelaysSet relays, string targetfile)
{ {
JsonSerializer serializer = new JsonSerializer(); JsonSerializer serializer = new JsonSerializer();
using (StreamWriter sw = new StreamWriter(@"" + root_folder + "" + targetfile)) using (StreamWriter sw = new StreamWriter(@"" + root_folder + "" + targetfile))

View file

@ -15,9 +15,18 @@ namespace BetterSecondBotShared.Json
public string[] CustomCommands { get; set; } public string[] CustomCommands { get; set; }
} }
public class JsonCustomRelaysSet
{
public JsonCustomRelays[] Entrys {get; set;}
}
public class JsonCustomRelays public class JsonCustomRelays
{ {
public string[] CustomRelays { get; set; } public bool encodeJson = false;
public string sourceType = "";
public string sourceFilter = "";
public string targetType = "";
public string targetConfig = "";
} }
public class advertConfig public class advertConfig