Skip to content

Commit

Permalink
0.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
kjsmita6 committed Dec 20, 2016
1 parent 06c84b5 commit eb2e830
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 22 deletions.
2 changes: 1 addition & 1 deletion SteamChatBot/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
8 changes: 8 additions & 0 deletions SteamChatBot/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions SteamChatBot/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<ListBoxItem x:Name="banCheckTrigger" Content="BanCheckTrigger - Checks SteamCommunity for bans"/>
<ListBoxItem x:Name="changeNameTrigger" Content="ChangeNameTrigger - Changes the bots name"/>
<ListBoxItem x:Name="chatReplyTrigger" Content="ChatReplyTrigger - Responds to a match in chat"/>
<ListBoxItem x:Name="chooseTrigger" Content="ChooseTrigger - Chooses one item from a list"/>
<ListBoxItem x:Name="discordTrigger" Content="DiscordTrigger - Discord Relay for Steam Chat"/>
<ListBoxItem x:Name="doormatTrigger" Content="DoormatTrigger - Greets a user as they enter chat"/>
<ListBoxItem x:Name="googleTrigger" Content="GoogleTrigger - Searches google and returns the top result"/>
Expand All @@ -75,6 +76,7 @@
<ListBoxItem x:Name="noteTrigger" Content="NoteTrigger - Saves notes than can be accessed by anyone in the chat"/>
<ListBoxItem x:Name="notificationTrigger" Content="NotificationTrigger - Sends users notifications"/>
<ListBoxItem x:Name="playGameTrigger" Content="PlayGameTrigger - Plays a game (AppID)"/>
<ListBoxItem x:Name="translateTrigger" Content="TranslateTrigger - Translates text from hablaa.com"/>
<ListBoxItem x:Name="unbanTrigger" Content="UnbanTrigger - Unbans a user from chat"/>
<ListBoxItem x:Name="unmoderateChatTrigger" Content="UnmoderateChatTrigger - Unmoderates the chat"/>
<ListBoxItem x:Name="weatherTrigger" Content="WeatherTrigger - Gets the weather for a certain location"/>
Expand Down
3 changes: 2 additions & 1 deletion SteamChatBot/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
48 changes: 48 additions & 0 deletions SteamChatBot/Parser.cs
Original file line number Diff line number Diff line change
@@ -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<T>(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<T>(this string @this) where T : class
{
return JSON.Deserialize<T>(@this.Trim());
}
}
}
3 changes: 3 additions & 0 deletions SteamChatBot/SteamChatBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,11 @@
<DependentUpon>AboutBox.cs</DependentUpon>
</Compile>
<Compile Include="Bot.cs" />
<Compile Include="Parser.cs" />
<Compile Include="Triggers\AntispamTrigger.cs" />
<Compile Include="Triggers\BanCheckTrigger.cs" />
<Compile Include="Triggers\ChangeNameTrigger.cs" />
<Compile Include="Triggers\ChooseTrigger.cs" />
<Compile Include="Triggers\DiscordTrigger.cs" />
<Compile Include="Triggers\GoogleTrigger.cs" />
<Compile Include="Triggers\LockChatTrigger.cs" />
Expand All @@ -296,6 +298,7 @@
<Compile Include="Triggers\NoteTrigger.cs" />
<Compile Include="Triggers\NotificationTrigger.cs" />
<Compile Include="Triggers\PlayGameTrigger.cs" />
<Compile Include="Triggers\TranslateTrigger.cs" />
<Compile Include="Triggers\TriggerOptions\AntiSpamTriggerOptions.cs" />
<Compile Include="Triggers\TriggerOptions\ChatCommand.cs" />
<Compile Include="Triggers\TriggerOptions\ChatCommandApi.cs" />
Expand Down
44 changes: 25 additions & 19 deletions SteamChatBot/Triggers/BaseTrigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public BaseTrigger(TriggerType type, string name, TriggerOptionsBase options)
/// <param name="name"></param>
/// <param name="error"></param>
/// <returns>error string</returns>
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
Expand Down Expand Up @@ -139,6 +139,9 @@ public static List<BaseTrigger> 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;
Expand Down Expand Up @@ -178,6 +181,9 @@ public static List<BaseTrigger> 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;
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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;
}
}
Expand All @@ -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;
}
}
Expand All @@ -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;
}
}
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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;
}
}
Expand All @@ -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;
}
}
Expand All @@ -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;
}
}
Expand All @@ -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;
}
}
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down
44 changes: 44 additions & 0 deletions SteamChatBot/Triggers/ChooseTrigger.cs
Original file line number Diff line number Diff line change
@@ -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<string> removed = new List<string>();
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;
}
}
}
Loading

0 comments on commit eb2e830

Please sign in to comment.