From eb2e830038afb1c24462b785592118e3466d06bd Mon Sep 17 00:00:00 2001 From: Kyle Smith Date: Mon, 19 Dec 2016 22:29:19 -0600 Subject: [PATCH] 0.7.0 --- SteamChatBot/Bot.cs | 2 +- SteamChatBot/Log.cs | 8 ++ SteamChatBot/MainWindow.xaml | 2 + SteamChatBot/MainWindow.xaml.cs | 3 +- SteamChatBot/Parser.cs | 48 ++++++++++++ SteamChatBot/SteamChatBot.csproj | 3 + SteamChatBot/Triggers/BaseTrigger.cs | 44 ++++++----- SteamChatBot/Triggers/ChooseTrigger.cs | 44 +++++++++++ SteamChatBot/Triggers/TranslateTrigger.cs | 95 +++++++++++++++++++++++ SteamChatBot/Triggers/TriggerType.cs | 4 +- 10 files changed, 231 insertions(+), 22 deletions(-) create mode 100644 SteamChatBot/Parser.cs create mode 100644 SteamChatBot/Triggers/ChooseTrigger.cs create mode 100644 SteamChatBot/Triggers/TranslateTrigger.cs diff --git a/SteamChatBot/Bot.cs b/SteamChatBot/Bot.cs index 4ba80c5..25e316a 100644 --- a/SteamChatBot/Bot.cs +++ b/SteamChatBot/Bot.cs @@ -98,7 +98,7 @@ public static void WriteData() File.Create("chatbots.txt"); } - if (!File.ReadAllText("chatbots.txt").Contains(username + "\n")) + if (!File.ReadAllText("chatbots.txt").Contains(username)) { File.AppendAllText("chatbots.txt", username + "\n"); } diff --git a/SteamChatBot/Log.cs b/SteamChatBot/Log.cs index 2998aa2..3fd5eb6 100644 --- a/SteamChatBot/Log.cs +++ b/SteamChatBot/Log.cs @@ -171,6 +171,14 @@ protected void _OutputLineToConsole(LogLevel level, string line) Console.ForegroundColor = _LogColor(level); Console.WriteLine(line); Console.ForegroundColor = DefaultConsoleColor; + if(level == LogLevel.Error) + { + if(!Directory.Exists(Bot.username + "/logs/errors")) + { + Directory.CreateDirectory(Bot.username + "/logs/errors"); + } + File.WriteAllText(Bot.username + "/logs/errors/err_" + (long)(DateTime.Now - new DateTime(1970, 1, 1)).TotalMilliseconds + ".txt", line); + } } // Determine the string equivalent of the LogLevel. diff --git a/SteamChatBot/MainWindow.xaml b/SteamChatBot/MainWindow.xaml index aaa3e7b..46c4b94 100644 --- a/SteamChatBot/MainWindow.xaml +++ b/SteamChatBot/MainWindow.xaml @@ -62,6 +62,7 @@ + @@ -75,6 +76,7 @@ + diff --git a/SteamChatBot/MainWindow.xaml.cs b/SteamChatBot/MainWindow.xaml.cs index 8978517..bc2fb13 100644 --- a/SteamChatBot/MainWindow.xaml.cs +++ b/SteamChatBot/MainWindow.xaml.cs @@ -247,7 +247,8 @@ private void plusTriggerButton_Click(object sender, RoutedEventArgs e) if (selected == "isUpTrigger" || selected == "leaveChatTrigger" || selected == "kickTrigger" || selected == "banTrigger" || selected == "unbanTrigger" || selected == "lockTrigger" || selected == "unlockTrigger" || selected == "moderateTrigger" || selected == "unmoderateTrigger" - || selected == "playGameTrigger" || selected == "changeNameTrigger" || selected == "googleTrigger") + || selected == "playGameTrigger" || selected == "changeNameTrigger" || selected == "googleTrigger" + || selected == "chooseTrigger" || selected == "translateTrigger") { ChatCommandWindow ccw = new ChatCommandWindow(); ccw.ShowDialog(); diff --git a/SteamChatBot/Parser.cs b/SteamChatBot/Parser.cs new file mode 100644 index 0000000..7f218e7 --- /dev/null +++ b/SteamChatBot/Parser.cs @@ -0,0 +1,48 @@ +using System; +using System.IO; +using System.Web.Script.Serialization; +using System.Xml; +using System.Xml.Serialization; + +namespace SteamChatBot +{ + public static class Parser + { + private static JavaScriptSerializer json; + private static JavaScriptSerializer JSON + { + get + { + return json ?? (json = new JavaScriptSerializer()); + } + } + + public static Stream ToStream(this string @this) + { + MemoryStream stream = new MemoryStream(); + StreamWriter writer = new StreamWriter(stream); + writer.Write(@this); + writer.Flush(); + stream.Position = 0; + return stream; + } + + public static T ParseXML(this string @this) where T : class + { + XmlRootAttribute root = new XmlRootAttribute(); + root.ElementName = "profile"; + root.IsNullable = true; + + var reader = XmlReader.Create(@this.Trim().ToStream(), new XmlReaderSettings() + { + ConformanceLevel = ConformanceLevel.Document + }); + return new XmlSerializer(typeof(T), root).Deserialize(reader) as T; + } + + public static T ParseJSON(this string @this) where T : class + { + return JSON.Deserialize(@this.Trim()); + } + } +} diff --git a/SteamChatBot/SteamChatBot.csproj b/SteamChatBot/SteamChatBot.csproj index 99023fc..aa3b0e3 100644 --- a/SteamChatBot/SteamChatBot.csproj +++ b/SteamChatBot/SteamChatBot.csproj @@ -273,9 +273,11 @@ AboutBox.cs + + @@ -296,6 +298,7 @@ + diff --git a/SteamChatBot/Triggers/BaseTrigger.cs b/SteamChatBot/Triggers/BaseTrigger.cs index 7898b1b..e7dc8fe 100644 --- a/SteamChatBot/Triggers/BaseTrigger.cs +++ b/SteamChatBot/Triggers/BaseTrigger.cs @@ -47,9 +47,9 @@ public BaseTrigger(TriggerType type, string name, TriggerOptionsBase options) /// /// /// error string - protected string IfError(string cbn, string name, string error) + protected string IfError(string cbn, string name, Exception error) { - return string.Format("{0}/{1}: Error: {2}", cbn, name, error); + return string.Format("{0}/{1}: {2}: {3}", cbn, name, error.Message, error.StackTrace); } #region trigger read-write @@ -139,6 +139,9 @@ public static List ReadTriggers() case TriggerType.ChatReplyTrigger: temp.Add(new ChatReplyTrigger(type, name, options)); break; + case TriggerType.ChooseTrigger: + temp.Add(new ChooseTrigger(type, name, options)); + break; case TriggerType.DiscordTrigger: temp.Add(new DiscordTrigger(type, name, options)); break; @@ -178,6 +181,9 @@ public static List ReadTriggers() case TriggerType.PlayGameTrigger: temp.Add(new PlayGameTrigger(type, name, options)); break; + case TriggerType.TranslateTrigger: + temp.Add(new TranslateTrigger(type, name, options)); + break; case TriggerType.UnbanTrigger: temp.Add(new UnbanTrigger(type, name, options)); break; @@ -224,7 +230,7 @@ public virtual bool OnLoad() } catch (Exception e) { - Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace)); + Log.Instance.Error(IfError(Bot.username, Name, e)); return false; } } @@ -241,7 +247,7 @@ public virtual bool OnLoggedOn() } catch (Exception e) { - Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace)); + Log.Instance.Error(IfError(Bot.username, Name, e)); return false; } } @@ -258,7 +264,7 @@ public virtual bool OnLoggedOff() } catch (Exception e) { - Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace)); + Log.Instance.Error(IfError(Bot.username, Name, e)); return false; } } @@ -280,7 +286,7 @@ public virtual bool OnChatInvite(SteamID roomID, string roomName, SteamID invite } catch (Exception e) { - Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace)); + Log.Instance.Error(IfError(Bot.username, Name, e)); return false; } } @@ -300,7 +306,7 @@ public virtual bool OnFriendRequest(SteamID userID) } catch (Exception e) { - Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace)); + Log.Instance.Error(IfError(Bot.username, Name, e)); return false; } } @@ -328,7 +334,7 @@ public virtual bool OnFriendMessage(SteamID userID, string message, bool haveSen } catch (Exception e) { - Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace)); + Log.Instance.Error(IfError(Bot.username, Name, e)); return false; } } @@ -354,7 +360,7 @@ public virtual bool OnTradeOffer(int number, bool haveEatenEvent) } catch (Exception e) { - Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace)); + Log.Instance.Error(IfError(Bot.username, Name, e)); return false; } } @@ -381,7 +387,7 @@ public virtual bool OnTradeProposed(SteamID tradeID, SteamID userID, bool haveEa } catch (Exception e) { - Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace)); + Log.Instance.Error(IfError(Bot.username, Name, e)); return false; } } @@ -407,7 +413,7 @@ public virtual bool OnTradeSession(SteamID userID, bool haveEatenEvent) } catch (Exception e) { - Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace)); + Log.Instance.Error(IfError(Bot.username, Name, e)); return false; } } @@ -434,7 +440,7 @@ public virtual bool OnAnnouncement(SteamID groupID, string headline, bool haveEa } catch (Exception e) { - Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace)); + Log.Instance.Error(IfError(Bot.username, Name, e)); return false; } } @@ -461,7 +467,7 @@ public virtual bool OnSentMessage(SteamID toID, string message, bool haveSentMes } catch (Exception e) { - Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace)); + Log.Instance.Error(IfError(Bot.username, Name, e)); return false; } } @@ -490,7 +496,7 @@ public virtual bool OnChatMessage(SteamID roomID, SteamID chatterID, string mess } catch (Exception e) { - Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace)); + Log.Instance.Error(IfError(Bot.username, Name, e)); return false; } } @@ -519,7 +525,7 @@ public virtual bool OnEnteredChat(SteamID roomID, SteamID userID, bool haveSentM } catch (Exception e) { - Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace)); + Log.Instance.Error(IfError(Bot.username, Name, e)); return false; } } @@ -548,7 +554,7 @@ public virtual bool OnKickedChat(SteamID roomID, SteamID kickedID, SteamID kicke } catch (Exception e) { - Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace)); + Log.Instance.Error(IfError(Bot.username, Name, e)); return false; } } @@ -577,7 +583,7 @@ public virtual bool OnBannedChat(SteamID roomID, SteamID bannedID, SteamID banne } catch (Exception e) { - Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace)); + Log.Instance.Error(IfError(Bot.username, Name, e)); return false; } } @@ -606,7 +612,7 @@ public virtual bool OnDisconnected(SteamID roomID, SteamID userID, bool haveSent } catch (Exception e) { - Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace)); + Log.Instance.Error(IfError(Bot.username, Name, e)); return false; } } @@ -634,7 +640,7 @@ public virtual bool OnLeftChat(SteamID roomID, SteamID userID) } catch (Exception e) { - Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace)); + Log.Instance.Error(IfError(Bot.username, Name, e)); return false; } } diff --git a/SteamChatBot/Triggers/ChooseTrigger.cs b/SteamChatBot/Triggers/ChooseTrigger.cs new file mode 100644 index 0000000..1990e55 --- /dev/null +++ b/SteamChatBot/Triggers/ChooseTrigger.cs @@ -0,0 +1,44 @@ +using SteamChatBot.Triggers.TriggerOptions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SteamKit2; + +namespace SteamChatBot.Triggers +{ + public class ChooseTrigger : BaseTrigger + { + public ChooseTrigger(TriggerType type, string name, TriggerOptionsBase options) : base(type, name, options) + { } + + public override bool respondToChatMessage(SteamID roomID, SteamID chatterId, string message) + { + return Respond(roomID, chatterId, message, true); + } + + public override bool respondToFriendMessage(SteamID userID, string message) + { + return Respond(userID, userID, message, false); + } + + private bool Respond(SteamID toID, SteamID userID, string message, bool room) + { + string[] query = StripCommand(message, Options.ChatCommand.Command); + if(query != null && query.Length > 2) + { + List removed = new List(); + for (int i = 1; i < query.Length; i++) + { + removed.Add(query[i]); + } + Random rng = new Random(); + string choice = removed[rng.Next(0, removed.Count)]; + SendMessageAfterDelay(toID, "I have chosen " + choice, room); + return true; + } + return false; + } + } +} diff --git a/SteamChatBot/Triggers/TranslateTrigger.cs b/SteamChatBot/Triggers/TranslateTrigger.cs new file mode 100644 index 0000000..1027545 --- /dev/null +++ b/SteamChatBot/Triggers/TranslateTrigger.cs @@ -0,0 +1,95 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SteamKit2; +using SteamChatBot.Triggers.TriggerOptions; +using System.Net; +using System.IO; + +namespace SteamChatBot.Triggers +{ + public class TranslateTrigger : BaseTrigger + { + class TranslateResult + { + public string text { get; set; } + public Pos pos { get; set; } + public string source { get; set; } + + public class Pos + { + public object code { get; set; } + public object title { get; set; } + } + } + + public TranslateTrigger(TriggerType type, string name, TriggerOptionsBase options) : base(type, name, options) + { } + + public override bool respondToChatMessage(SteamID roomID, SteamID chatterId, string message) + { + return Respond(roomID, chatterId, message, true); + } + + public override bool respondToFriendMessage(SteamID userID, string message) + { + return Respond(userID, userID, message, false); + } + + private bool Respond(SteamID toID, SteamID userID, string message, bool room) + { + string[] query = StripCommand(message, Options.ChatCommand.Command); + if (query != null && query.Length == 4) + { + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("http://hablaa.com/hs/translation/" + query[1] + "/" + query[2] + "-" + query[3] + "/")); + try { + using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) + { + if (response.StatusCode == HttpStatusCode.NotFound) + { + return SendNotFound(toID, query, room); + } + else if (response.StatusCode != HttpStatusCode.OK) + { + SendMessageAfterDelay(toID, "Status code from hablaa.com: " + response.StatusCode + "/" + response.StatusDescription, room); + return true; + } + using (StreamReader sr = new StreamReader(response.GetResponseStream())) + { + string text = sr.ReadToEnd(); + TranslateResult[] result = text.ParseJSON(); + if (result == null) + { + return SendNotFound(toID, query, room); + } + string trans = result[0].text; + if (trans == null || trans == "") + { + return SendNotFound(toID, query, room); + } + else + { + SendMessageAfterDelay(toID, trans, room); + return true; + } + } + } + } + catch(WebException we) + { + return SendNotFound(toID, query, room); + } + } + return false; + } + + private bool SendNotFound(SteamID toID, string[] query, bool room) + { + SendMessageAfterDelay(toID, "Either " + query[1] + " is not defined in that language or one of the languages you specified is incorrect. Please visit http://hablaa.com/hs/translation/" + + query[1] + "/" + query[2] + "-" + query[3] + " for more information.", room); + return true; + } + } +} diff --git a/SteamChatBot/Triggers/TriggerType.cs b/SteamChatBot/Triggers/TriggerType.cs index 42e71d7..69cbdc6 100644 --- a/SteamChatBot/Triggers/TriggerType.cs +++ b/SteamChatBot/Triggers/TriggerType.cs @@ -27,6 +27,8 @@ public enum TriggerType YoutubeTrigger, ChangeNameTrigger, GoogleTrigger, - MessageIntervalTrigger + MessageIntervalTrigger, + ChooseTrigger, + TranslateTrigger } }