Skip to content

Commit 7971f1d

Browse files
committed
Finish NoteTrigger
1 parent 59ec717 commit 7971f1d

7 files changed

Lines changed: 130 additions & 53 deletions

File tree

SteamChatBot/Bot.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ private static void SubForCB()
244244
manager.Subscribe<SteamUser.LoggedOnCallback>(OnLoggedOn);
245245
manager.Subscribe<SteamUser.LoggedOffCallback>(OnLoggedOff);
246246
manager.Subscribe<SteamUser.UpdateMachineAuthCallback>(OnUpdateMachineAuth);
247-
manager.Subscribe<SteamUser.AccountInfoCallback>(OnAccountInfo);
248247

249248
manager.Subscribe<SteamFriends.ChatMsgCallback>(OnChatMsg);
250249
manager.Subscribe<SteamFriends.FriendMsgCallback>(OnFriendMsg);
@@ -255,12 +254,6 @@ private static void SubForCB()
255254
Log.Instance.Silly("Callback managers subscribed");
256255
}
257256

258-
private static void OnAccountInfo(SteamUser.AccountInfoCallback callback)
259-
{
260-
steamFriends.SetPersonaState(EPersonaState.Online);
261-
steamFriends.SetPersonaName(displayName);
262-
}
263-
264257
private static void OnFriendMsg(SteamFriends.FriendMsgCallback callback)
265258
{
266259
if (callback.EntryType == EChatEntryType.ChatMsg)
@@ -340,6 +333,8 @@ private static void OnLoggedOn(SteamUser.LoggedOnCallback callback)
340333
if (callback.Result == EResult.OK)
341334
{
342335
Log.Instance.Info("Logged in!");
336+
steamFriends.SetPersonaState(EPersonaState.Online);
337+
steamFriends.SetPersonaName(displayName);
343338
foreach (BaseTrigger trigger in triggers)
344339
{
345340
trigger.OnLoggedOn();

SteamChatBot/MainWindow.xaml.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,10 @@ private NoteTriggerOptions GetNoteTriggerOptions(NoteTriggerOptions nto)
547547
NoCommand = nto.NoCommand,
548548
DeleteCommand = nto.DeleteCommand,
549549
InfoCommand = nto.InfoCommand,
550-
NoteCommand = nto.NoteCommand
550+
NoteCommand = nto.NoteCommand,
551+
SaveTimer = nto.SaveTimer,
552+
NoteFile = nto.NoteFile,
553+
NotesCommand = nto.NotesCommand
551554
};
552555
}
553556
catch (Exception e) { return null; }

SteamChatBot/Triggers/NoteTrigger.cs

Lines changed: 85 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,38 @@ public class NoteTrigger : BaseTrigger
1818

1919
public NoteTrigger(TriggerType type, string name, TriggerOptionsBase options) : base(type, name, options)
2020
{
21-
Options.NoteTriggerOptions.Notes = new Dictionary<SteamID, Dictionary<string, Note>>();
22-
saveNoteTimer = new Timer(1000 * 60 * 5);
21+
saveNoteTimer = new Timer(options.NoteTriggerOptions.SaveTimer);
2322
saveNoteTimer.Elapsed += SaveNoteTimer_Elapsed;
2423
}
2524

2625
private void SaveNoteTimer_Elapsed(object sender, ElapsedEventArgs e)
2726
{
2827
string json = JsonConvert.SerializeObject(Options.NoteTriggerOptions.Notes);
29-
File.WriteAllText(Bot.username + "/notes.json", json);
28+
File.WriteAllText(Options.NoteTriggerOptions.NoteFile, json);
29+
Log.Instance.Silly("{0}/{1}: Wrote notes to {0}/notes.json", Bot.username, Name);
3030
}
3131

3232
public override bool OnLoggedOn()
3333
{
3434
try
3535
{
36-
string notes = File.ReadAllText(Bot.username + "/notes.json");
37-
Options.NoteTriggerOptions.Notes = (Dictionary<SteamID, Dictionary<string, Note>>)JsonConvert.DeserializeObject(notes);
36+
Options.NoteTriggerOptions.Notes = JsonConvert.DeserializeObject<Dictionary<ulong, Dictionary<string, Note>>>(File.ReadAllText(Options.NoteTriggerOptions.NoteFile));
37+
Log.Instance.Silly(Bot.username + "/" + Name + ": Loaded notes from " + Options.NoteTriggerOptions.NoteFile);
3838
}
39-
catch(FileNotFoundException fnfe)
39+
catch (FileNotFoundException fnfe)
4040
{
41-
File.Create(Bot.username + "/notes.json");
41+
File.Create(Options.NoteTriggerOptions.NoteFile);
4242

4343
}
44-
catch(Exception e)
44+
catch (Exception e)
4545
{
46-
Log.Instance.Error(e.StackTrace);
46+
Log.Instance.Error(e.Message + ": " + e.StackTrace);
47+
}
48+
49+
if (Options.NoteTriggerOptions.Notes == null)
50+
{
51+
Options.NoteTriggerOptions.Notes = new Dictionary<ulong, Dictionary<string, Note>>();
4752
}
48-
4953

5054
saveNoteTimer.Start();
5155

@@ -59,18 +63,80 @@ public override bool respondToChatMessage(SteamID roomID, SteamID chatterId, str
5963

6064
private bool Respond(SteamID roomID, SteamID userID, string message)
6165
{
62-
string[] query = StripCommand(message, Options.NoteTriggerOptions.NoteCommand);
66+
Dictionary<string, Note> db;
67+
ulong room = roomID.ConvertToUInt64();
68+
69+
if (!Options.NoteTriggerOptions.Notes.ContainsKey(room))
70+
{
71+
Options.NoteTriggerOptions.Notes[room] = new Dictionary<string, Note>();
72+
db = Options.NoteTriggerOptions.Notes[room];
73+
}
74+
else
75+
{
76+
db = Options.NoteTriggerOptions.Notes[room];
77+
}
78+
79+
string[] query = StripCommand(message, Options.NoteTriggerOptions.NotesCommand);
80+
if(query != null && query.Length == 1)
81+
{
82+
List<string> _notes = new List<string>();
83+
string notes = "";
84+
foreach(string note in db.Keys)
85+
{
86+
_notes.Add(note);
87+
}
88+
notes = string.Join(", ", _notes.ToArray());
89+
SendMessageAfterDelay(roomID, "Please see your personal chat for a list of all the notes in this chat room", true);
90+
SendMessageAfterDelay(userID, "Notes in " + roomID.ConvertToUInt64() + ": " + notes, false);
91+
return true;
92+
}
93+
94+
query = StripCommand(message, Options.NoteTriggerOptions.DeleteCommand);
95+
if (query != null && query.Length == 2)
96+
{
97+
string name = query[1];
98+
if (!db.ContainsKey(name))
99+
{
100+
SendMessageAfterDelay(roomID, string.Format("The note \"{0}\" does not exist. Use \"{1}\" to create it.", name, Options.NoteTriggerOptions.NoteCommand + " " + name + " <definition>"), true);
101+
return true;
102+
}
103+
else
104+
{
105+
db.Remove(name);
106+
SendMessageAfterDelay(roomID, string.Format("Note \"{0}\" deleted", name), true);
107+
return true;
108+
}
109+
}
110+
111+
query = StripCommand(message, Options.NoteTriggerOptions.InfoCommand);
112+
if(query != null && query.Length == 2)
113+
{
114+
string name = query[1];
115+
if (!db.ContainsKey(name))
116+
{
117+
SendMessageAfterDelay(roomID, string.Format("The note \"{0}\" does not exist. Use \"{1}\" to create it.", name, Options.NoteTriggerOptions.NoteCommand + " " + name + " <definition>"), true);
118+
return true;
119+
}
120+
else
121+
{
122+
Note note = db[name];
123+
SendMessageAfterDelay(roomID, string.Format("The note \"{0}\" with definition \"{1}\" was last modified by {2} on {3}", name, note.Definition, note.ModifiedBy, note.ModifiedWhen), true);
124+
return true;
125+
}
126+
}
127+
128+
query = StripCommand(message, Options.NoteTriggerOptions.NoteCommand);
63129
if (query != null && query.Length == 2)
64130
{
65131
string name = query[1];
66-
Note note = Options.NoteTriggerOptions.Notes[roomID][name];
67-
if(note == null)
132+
if (!db.ContainsKey(name))
68133
{
69134
SendMessageAfterDelay(roomID, string.Format("The note \"{0}\" does not exist. Use \"{1}\" to create it.", name, Options.NoteTriggerOptions.NoteCommand + " " + name + " <definition>"), true);
70135
return true;
71136
}
72137
else
73138
{
139+
Note note = db[name];
74140
SendMessageAfterDelay(roomID, "\"" + note.Definition + "\"", true);
75141
return true;
76142
}
@@ -90,38 +156,23 @@ private bool Respond(SteamID roomID, SteamID userID, string message)
90156
string definition = string.Join(" ", def);
91157

92158
note.Definition = definition;
93-
note.ModifiedBy = Bot.steamFriends.GetFriendPersonaName(userID) + " <" + userID.ConvertToUInt64().ToString() + ">";
159+
note.ModifiedBy = Bot.steamFriends.GetFriendPersonaName(userID) + " (" + userID.ConvertToUInt64().ToString() + ")";
94160
note.ModifiedWhen = DateTime.Now.ToString();
95161

96-
Dictionary<string, Note> innerNote = new Dictionary<string, Note>();
97-
innerNote.Add(name, note);
98-
99-
if (Options.NoteTriggerOptions.Notes == null || Options.NoteTriggerOptions.Notes[roomID] == null || Options.NoteTriggerOptions.Notes[roomID][name] == null)
162+
if (!db.ContainsKey(name))
100163
{
101-
Options.NoteTriggerOptions.Notes = new Dictionary<SteamID, Dictionary<string, Note>>();
102-
Options.NoteTriggerOptions.Notes.Add(roomID, innerNote);
164+
db.Add(name, note);
103165
}
104166
else
105167
{
106-
Options.NoteTriggerOptions.Notes.Remove(roomID);
107-
Options.NoteTriggerOptions.Notes.Add(roomID, innerNote);
168+
db.Remove(name);
169+
db.Add(name, note);
108170
}
109171

110172
SendMessageAfterDelay(roomID, string.Format("Note \"{0}\" saved", name), true);
111173
return true;
112174
}
113-
114-
query = StripCommand(message, Options.NoteTriggerOptions.DeleteCommand);
115-
if(query != null && query.Length == 2)
116-
{
117-
string name = query[1];
118-
Options.NoteTriggerOptions.Notes[roomID].Remove(name);
119-
SendMessageAfterDelay(roomID, string.Format("Note \"{0}\" deleted", name), true);
120-
return true;
121-
}
122175
return false;
123-
124-
125176
}
126177
}
127-
}
178+
}

SteamChatBot/Triggers/TriggerOptions/NoteTriggerOptions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ public class NoteTriggerOptions
99
public string NoteCommand { get; set; }
1010
public string InfoCommand { get; set; }
1111
public string DeleteCommand { get; set; }
12+
public string NotesCommand { get; set; }
1213
public NoCommand NoCommand { get; set; }
13-
public Dictionary<SteamID, Dictionary<string, Note>> Notes { get; set; }
14+
public Dictionary<ulong, Dictionary<string, Note>> Notes { get; set; }
15+
public int SaveTimer { get; set; }
16+
public string NoteFile { get; set; }
1417
}
1518

1619
public class Note

SteamChatBot/Triggers/TriggerOptions/Windows/AntiSpamTriggerOptionsWindow.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<Button x:Name="doneButton" Content="Done" HorizontalAlignment="Left" Margin="278,126,0,0" VerticalAlignment="Top" Width="87" Height="81" Click="doneButton_Click"/>
2727
<Label x:Name="label" Content="Admins:" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
2828
<TextBox x:Name="adminsBox" HorizontalAlignment="Left" Height="23" Margin="117,13,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
29-
<Label x:Name="label_Copy4" Content="Resolution:" HorizontalAlignment="Left" Margin="10,94,0,0" VerticalAlignment="Top"/>
29+
<Label x:Name="label_Copy4" Content="Resolution (ms):" HorizontalAlignment="Left" Margin="10,94,0,0" VerticalAlignment="Top"/>
3030
<TextBox x:Name="resolutionBox" HorizontalAlignment="Left" Height="23" Margin="117,97,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
3131
<Label x:Name="label_Copy10" Content="Resolution Amount:" HorizontalAlignment="Left" Margin="10,120,0,0" VerticalAlignment="Top"/>
3232
<TextBox x:Name="amountBox" HorizontalAlignment="Left" Height="23" Margin="129,123,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="108"/>

SteamChatBot/Triggers/TriggerOptions/Windows/NoteTriggerOptionsWindow.xaml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@
77
mc:Ignorable="d"
88
Title="Options" Height="300" Width="300" Icon="/SteamChatBot;component/scb.ico">
99
<Grid>
10-
<Button x:Name="doneButton" Content="Done" HorizontalAlignment="Left" Margin="84,179,0,0" VerticalAlignment="Top" Width="126" Height="50" Click="doneButton_Click"/>
10+
<Button x:Name="doneButton" Content="Done" HorizontalAlignment="Left" Margin="85,210,0,0" VerticalAlignment="Top" Width="126" Height="50" Click="doneButton_Click"/>
1111
<Label x:Name="noteCommandLabel" Content="Note command:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="24,28,0,0"/>
12-
<TextBox x:Name="noteCommandBox" HorizontalAlignment="Left" Height="23" Margin="159,31,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="86"/>
13-
<Label x:Name="infoCommandLabel" Content="Note info command:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="24,68,0,0"/>
14-
<TextBox x:Name="infoCommandBox" HorizontalAlignment="Left" Height="23" Margin="159,71,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="86"/>
15-
<Label x:Name="noteDeleteCommandLabel" Content="Note delete command:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="24,110,0,0"/>
16-
<TextBox x:Name="deleteCommandBox" HorizontalAlignment="Left" Height="23" Margin="159,113,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="86"/>
12+
<TextBox x:Name="noteCommandBox" HorizontalAlignment="Left" Height="23" Margin="182,31,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="86"/>
13+
<Label x:Name="infoCommandLabel" Content="Note info command:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="24,56,0,0"/>
14+
<TextBox x:Name="infoCommandBox" HorizontalAlignment="Left" Height="23" Margin="182,59,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="86"/>
15+
<Label x:Name="noteDeleteCommandLabel" Content="Note delete command:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="24,84,0,0"/>
16+
<TextBox x:Name="deleteCommandBox" HorizontalAlignment="Left" Height="23" Margin="182,87,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="86" RenderTransformOrigin="0.518,-0.159"/>
17+
<Label x:Name="noteFileLabel" Content="Note file:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="24,138,0,0"/>
18+
<TextBox x:Name="noteFileBox" HorizontalAlignment="Left" Height="23" Margin="182,143,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="86" GotFocus="noteFileBox_GotFocus"/>
19+
<Label x:Name="saveTimerLabel" Content="Save timer (ms):" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="24,169,0,0"/>
20+
<TextBox x:Name="saveTimerBox" HorizontalAlignment="Left" Height="23" Margin="182,171,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="86"/>
21+
<Label x:Name="notesCommandLabel" Content="All notes command:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="24,112,0,0"/>
22+
<TextBox x:Name="notesCommandBox" HorizontalAlignment="Left" Height="23" Margin="182,115,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="86" RenderTransformOrigin="0.518,-0.159"/>
1723

1824
</Grid>
1925
</Window>

SteamChatBot/Triggers/TriggerOptions/Windows/NoteTriggerOptionsWindow.xaml.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Microsoft.Win32;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Text;
@@ -31,24 +32,42 @@ public NoteTriggerOptionsWindow()
3132
private void doneButton_Click(object sender, RoutedEventArgs e)
3233
{
3334
ncw.ShowDialog();
34-
if(ncw.DialogResult.HasValue && ncw.DialogResult == true)
35+
if (ncw.DialogResult.HasValue && ncw.DialogResult == true)
3536
{
3637
NC = ncw.NC;
3738
NTO = new NoteTriggerOptions
3839
{
3940
Name = NC.Name,
4041
NoteCommand = "!note",
4142
InfoCommand = "!note_info",
42-
DeleteCommand = "!note_delete"
43+
DeleteCommand = "!note_delete",
44+
SaveTimer = 1000 * 60 * 5,
45+
NoteFile = AppDomain.CurrentDomain.BaseDirectory + "/notes.json",
46+
NotesCommand = "!notes"
4347
};
4448

4549
if (noteCommandBox.Text != "") NTO.NoteCommand = noteCommandBox.Text;
4650
if (infoCommandBox.Text != "") NTO.InfoCommand = infoCommandBox.Text;
4751
if (deleteCommandBox.Text != "") NTO.DeleteCommand = deleteCommandBox.Text;
52+
if (saveTimerBox.Text != "") NTO.SaveTimer = Convert.ToInt32(saveTimerBox.Text);
53+
if (noteFileBox.Text != "") NTO.NoteFile = noteFileBox.Text;
54+
if (notesCommandBox.Text != "") NTO.NotesCommand = notesCommandBox.Text;
4855

4956
DialogResult = true;
5057
Close();
5158
}
5259
}
60+
61+
private void noteFileBox_GotFocus(object sender, RoutedEventArgs e)
62+
{
63+
string file = "";
64+
OpenFileDialog ofd = new OpenFileDialog();
65+
66+
if(ofd.ShowDialog() == true)
67+
{
68+
file = ofd.FileName;
69+
noteFileBox.Text = ofd.FileName;
70+
}
71+
}
5372
}
5473
}

0 commit comments

Comments
 (0)