Skip to content

Commit eb2e830

Browse files
committed
0.7.0
1 parent 06c84b5 commit eb2e830

10 files changed

Lines changed: 231 additions & 22 deletions

File tree

SteamChatBot/Bot.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public static void WriteData()
9898
File.Create("chatbots.txt");
9999
}
100100

101-
if (!File.ReadAllText("chatbots.txt").Contains(username + "\n"))
101+
if (!File.ReadAllText("chatbots.txt").Contains(username))
102102
{
103103
File.AppendAllText("chatbots.txt", username + "\n");
104104
}

SteamChatBot/Log.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,14 @@ protected void _OutputLineToConsole(LogLevel level, string line)
171171
Console.ForegroundColor = _LogColor(level);
172172
Console.WriteLine(line);
173173
Console.ForegroundColor = DefaultConsoleColor;
174+
if(level == LogLevel.Error)
175+
{
176+
if(!Directory.Exists(Bot.username + "/logs/errors"))
177+
{
178+
Directory.CreateDirectory(Bot.username + "/logs/errors");
179+
}
180+
File.WriteAllText(Bot.username + "/logs/errors/err_" + (long)(DateTime.Now - new DateTime(1970, 1, 1)).TotalMilliseconds + ".txt", line);
181+
}
174182
}
175183

176184
// Determine the string equivalent of the LogLevel.

SteamChatBot/MainWindow.xaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
<ListBoxItem x:Name="banCheckTrigger" Content="BanCheckTrigger - Checks SteamCommunity for bans"/>
6363
<ListBoxItem x:Name="changeNameTrigger" Content="ChangeNameTrigger - Changes the bots name"/>
6464
<ListBoxItem x:Name="chatReplyTrigger" Content="ChatReplyTrigger - Responds to a match in chat"/>
65+
<ListBoxItem x:Name="chooseTrigger" Content="ChooseTrigger - Chooses one item from a list"/>
6566
<ListBoxItem x:Name="discordTrigger" Content="DiscordTrigger - Discord Relay for Steam Chat"/>
6667
<ListBoxItem x:Name="doormatTrigger" Content="DoormatTrigger - Greets a user as they enter chat"/>
6768
<ListBoxItem x:Name="googleTrigger" Content="GoogleTrigger - Searches google and returns the top result"/>
@@ -75,6 +76,7 @@
7576
<ListBoxItem x:Name="noteTrigger" Content="NoteTrigger - Saves notes than can be accessed by anyone in the chat"/>
7677
<ListBoxItem x:Name="notificationTrigger" Content="NotificationTrigger - Sends users notifications"/>
7778
<ListBoxItem x:Name="playGameTrigger" Content="PlayGameTrigger - Plays a game (AppID)"/>
79+
<ListBoxItem x:Name="translateTrigger" Content="TranslateTrigger - Translates text from hablaa.com"/>
7880
<ListBoxItem x:Name="unbanTrigger" Content="UnbanTrigger - Unbans a user from chat"/>
7981
<ListBoxItem x:Name="unmoderateChatTrigger" Content="UnmoderateChatTrigger - Unmoderates the chat"/>
8082
<ListBoxItem x:Name="weatherTrigger" Content="WeatherTrigger - Gets the weather for a certain location"/>

SteamChatBot/MainWindow.xaml.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ private void plusTriggerButton_Click(object sender, RoutedEventArgs e)
247247
if (selected == "isUpTrigger" || selected == "leaveChatTrigger" || selected == "kickTrigger"
248248
|| selected == "banTrigger" || selected == "unbanTrigger" || selected == "lockTrigger"
249249
|| selected == "unlockTrigger" || selected == "moderateTrigger" || selected == "unmoderateTrigger"
250-
|| selected == "playGameTrigger" || selected == "changeNameTrigger" || selected == "googleTrigger")
250+
|| selected == "playGameTrigger" || selected == "changeNameTrigger" || selected == "googleTrigger"
251+
|| selected == "chooseTrigger" || selected == "translateTrigger")
251252
{
252253
ChatCommandWindow ccw = new ChatCommandWindow();
253254
ccw.ShowDialog();

SteamChatBot/Parser.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.IO;
3+
using System.Web.Script.Serialization;
4+
using System.Xml;
5+
using System.Xml.Serialization;
6+
7+
namespace SteamChatBot
8+
{
9+
public static class Parser
10+
{
11+
private static JavaScriptSerializer json;
12+
private static JavaScriptSerializer JSON
13+
{
14+
get
15+
{
16+
return json ?? (json = new JavaScriptSerializer());
17+
}
18+
}
19+
20+
public static Stream ToStream(this string @this)
21+
{
22+
MemoryStream stream = new MemoryStream();
23+
StreamWriter writer = new StreamWriter(stream);
24+
writer.Write(@this);
25+
writer.Flush();
26+
stream.Position = 0;
27+
return stream;
28+
}
29+
30+
public static T ParseXML<T>(this string @this) where T : class
31+
{
32+
XmlRootAttribute root = new XmlRootAttribute();
33+
root.ElementName = "profile";
34+
root.IsNullable = true;
35+
36+
var reader = XmlReader.Create(@this.Trim().ToStream(), new XmlReaderSettings()
37+
{
38+
ConformanceLevel = ConformanceLevel.Document
39+
});
40+
return new XmlSerializer(typeof(T), root).Deserialize(reader) as T;
41+
}
42+
43+
public static T ParseJSON<T>(this string @this) where T : class
44+
{
45+
return JSON.Deserialize<T>(@this.Trim());
46+
}
47+
}
48+
}

SteamChatBot/SteamChatBot.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,11 @@
273273
<DependentUpon>AboutBox.cs</DependentUpon>
274274
</Compile>
275275
<Compile Include="Bot.cs" />
276+
<Compile Include="Parser.cs" />
276277
<Compile Include="Triggers\AntispamTrigger.cs" />
277278
<Compile Include="Triggers\BanCheckTrigger.cs" />
278279
<Compile Include="Triggers\ChangeNameTrigger.cs" />
280+
<Compile Include="Triggers\ChooseTrigger.cs" />
279281
<Compile Include="Triggers\DiscordTrigger.cs" />
280282
<Compile Include="Triggers\GoogleTrigger.cs" />
281283
<Compile Include="Triggers\LockChatTrigger.cs" />
@@ -296,6 +298,7 @@
296298
<Compile Include="Triggers\NoteTrigger.cs" />
297299
<Compile Include="Triggers\NotificationTrigger.cs" />
298300
<Compile Include="Triggers\PlayGameTrigger.cs" />
301+
<Compile Include="Triggers\TranslateTrigger.cs" />
299302
<Compile Include="Triggers\TriggerOptions\AntiSpamTriggerOptions.cs" />
300303
<Compile Include="Triggers\TriggerOptions\ChatCommand.cs" />
301304
<Compile Include="Triggers\TriggerOptions\ChatCommandApi.cs" />

SteamChatBot/Triggers/BaseTrigger.cs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ public BaseTrigger(TriggerType type, string name, TriggerOptionsBase options)
4747
/// <param name="name"></param>
4848
/// <param name="error"></param>
4949
/// <returns>error string</returns>
50-
protected string IfError(string cbn, string name, string error)
50+
protected string IfError(string cbn, string name, Exception error)
5151
{
52-
return string.Format("{0}/{1}: Error: {2}", cbn, name, error);
52+
return string.Format("{0}/{1}: {2}: {3}", cbn, name, error.Message, error.StackTrace);
5353
}
5454

5555
#region trigger read-write
@@ -139,6 +139,9 @@ public static List<BaseTrigger> ReadTriggers()
139139
case TriggerType.ChatReplyTrigger:
140140
temp.Add(new ChatReplyTrigger(type, name, options));
141141
break;
142+
case TriggerType.ChooseTrigger:
143+
temp.Add(new ChooseTrigger(type, name, options));
144+
break;
142145
case TriggerType.DiscordTrigger:
143146
temp.Add(new DiscordTrigger(type, name, options));
144147
break;
@@ -178,6 +181,9 @@ public static List<BaseTrigger> ReadTriggers()
178181
case TriggerType.PlayGameTrigger:
179182
temp.Add(new PlayGameTrigger(type, name, options));
180183
break;
184+
case TriggerType.TranslateTrigger:
185+
temp.Add(new TranslateTrigger(type, name, options));
186+
break;
181187
case TriggerType.UnbanTrigger:
182188
temp.Add(new UnbanTrigger(type, name, options));
183189
break;
@@ -224,7 +230,7 @@ public virtual bool OnLoad()
224230
}
225231
catch (Exception e)
226232
{
227-
Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace));
233+
Log.Instance.Error(IfError(Bot.username, Name, e));
228234
return false;
229235
}
230236
}
@@ -241,7 +247,7 @@ public virtual bool OnLoggedOn()
241247
}
242248
catch (Exception e)
243249
{
244-
Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace));
250+
Log.Instance.Error(IfError(Bot.username, Name, e));
245251
return false;
246252
}
247253
}
@@ -258,7 +264,7 @@ public virtual bool OnLoggedOff()
258264
}
259265
catch (Exception e)
260266
{
261-
Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace));
267+
Log.Instance.Error(IfError(Bot.username, Name, e));
262268
return false;
263269
}
264270
}
@@ -280,7 +286,7 @@ public virtual bool OnChatInvite(SteamID roomID, string roomName, SteamID invite
280286
}
281287
catch (Exception e)
282288
{
283-
Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace));
289+
Log.Instance.Error(IfError(Bot.username, Name, e));
284290
return false;
285291
}
286292
}
@@ -300,7 +306,7 @@ public virtual bool OnFriendRequest(SteamID userID)
300306
}
301307
catch (Exception e)
302308
{
303-
Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace));
309+
Log.Instance.Error(IfError(Bot.username, Name, e));
304310
return false;
305311
}
306312
}
@@ -328,7 +334,7 @@ public virtual bool OnFriendMessage(SteamID userID, string message, bool haveSen
328334
}
329335
catch (Exception e)
330336
{
331-
Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace));
337+
Log.Instance.Error(IfError(Bot.username, Name, e));
332338
return false;
333339
}
334340
}
@@ -354,7 +360,7 @@ public virtual bool OnTradeOffer(int number, bool haveEatenEvent)
354360
}
355361
catch (Exception e)
356362
{
357-
Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace));
363+
Log.Instance.Error(IfError(Bot.username, Name, e));
358364
return false;
359365
}
360366
}
@@ -381,7 +387,7 @@ public virtual bool OnTradeProposed(SteamID tradeID, SteamID userID, bool haveEa
381387
}
382388
catch (Exception e)
383389
{
384-
Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace));
390+
Log.Instance.Error(IfError(Bot.username, Name, e));
385391
return false;
386392
}
387393
}
@@ -407,7 +413,7 @@ public virtual bool OnTradeSession(SteamID userID, bool haveEatenEvent)
407413
}
408414
catch (Exception e)
409415
{
410-
Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace));
416+
Log.Instance.Error(IfError(Bot.username, Name, e));
411417
return false;
412418
}
413419
}
@@ -434,7 +440,7 @@ public virtual bool OnAnnouncement(SteamID groupID, string headline, bool haveEa
434440
}
435441
catch (Exception e)
436442
{
437-
Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace));
443+
Log.Instance.Error(IfError(Bot.username, Name, e));
438444
return false;
439445
}
440446
}
@@ -461,7 +467,7 @@ public virtual bool OnSentMessage(SteamID toID, string message, bool haveSentMes
461467
}
462468
catch (Exception e)
463469
{
464-
Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace));
470+
Log.Instance.Error(IfError(Bot.username, Name, e));
465471
return false;
466472
}
467473
}
@@ -490,7 +496,7 @@ public virtual bool OnChatMessage(SteamID roomID, SteamID chatterID, string mess
490496
}
491497
catch (Exception e)
492498
{
493-
Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace));
499+
Log.Instance.Error(IfError(Bot.username, Name, e));
494500
return false;
495501
}
496502
}
@@ -519,7 +525,7 @@ public virtual bool OnEnteredChat(SteamID roomID, SteamID userID, bool haveSentM
519525
}
520526
catch (Exception e)
521527
{
522-
Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace));
528+
Log.Instance.Error(IfError(Bot.username, Name, e));
523529
return false;
524530
}
525531
}
@@ -548,7 +554,7 @@ public virtual bool OnKickedChat(SteamID roomID, SteamID kickedID, SteamID kicke
548554
}
549555
catch (Exception e)
550556
{
551-
Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace));
557+
Log.Instance.Error(IfError(Bot.username, Name, e));
552558
return false;
553559
}
554560
}
@@ -577,7 +583,7 @@ public virtual bool OnBannedChat(SteamID roomID, SteamID bannedID, SteamID banne
577583
}
578584
catch (Exception e)
579585
{
580-
Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace));
586+
Log.Instance.Error(IfError(Bot.username, Name, e));
581587
return false;
582588
}
583589
}
@@ -606,7 +612,7 @@ public virtual bool OnDisconnected(SteamID roomID, SteamID userID, bool haveSent
606612
}
607613
catch (Exception e)
608614
{
609-
Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace));
615+
Log.Instance.Error(IfError(Bot.username, Name, e));
610616
return false;
611617
}
612618
}
@@ -634,7 +640,7 @@ public virtual bool OnLeftChat(SteamID roomID, SteamID userID)
634640
}
635641
catch (Exception e)
636642
{
637-
Log.Instance.Error(IfError(Bot.username, Name, e.StackTrace));
643+
Log.Instance.Error(IfError(Bot.username, Name, e));
638644
return false;
639645
}
640646
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using SteamChatBot.Triggers.TriggerOptions;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using SteamKit2;
8+
9+
namespace SteamChatBot.Triggers
10+
{
11+
public class ChooseTrigger : BaseTrigger
12+
{
13+
public ChooseTrigger(TriggerType type, string name, TriggerOptionsBase options) : base(type, name, options)
14+
{ }
15+
16+
public override bool respondToChatMessage(SteamID roomID, SteamID chatterId, string message)
17+
{
18+
return Respond(roomID, chatterId, message, true);
19+
}
20+
21+
public override bool respondToFriendMessage(SteamID userID, string message)
22+
{
23+
return Respond(userID, userID, message, false);
24+
}
25+
26+
private bool Respond(SteamID toID, SteamID userID, string message, bool room)
27+
{
28+
string[] query = StripCommand(message, Options.ChatCommand.Command);
29+
if(query != null && query.Length > 2)
30+
{
31+
List<string> removed = new List<string>();
32+
for (int i = 1; i < query.Length; i++)
33+
{
34+
removed.Add(query[i]);
35+
}
36+
Random rng = new Random();
37+
string choice = removed[rng.Next(0, removed.Count)];
38+
SendMessageAfterDelay(toID, "I have chosen " + choice, room);
39+
return true;
40+
}
41+
return false;
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)