-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
174 lines (146 loc) · 5.22 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System;
using MargieBot;
using Kerstbot.Responsers;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
using System.IO;
namespace Kerstbot;
class Program
{
private static string _token;
private static bool _running;
static async Task Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("Usage: kerstbot.exe <token>");
return;
}
Console.WriteLine("Kerstbot v1.2");
_token = args[0];
var replyResponder = new WittyReplyResponder();
var bot = CreateBot(replyResponder);
_running = await bot.TryConnect(_token);
if (!_running)
{
Console.WriteLine("Connection failed...");
return;
}
var tokenSource = new CancellationTokenSource();
var consoleTask = Task.Run(() => ConsoleTask(bot, tokenSource));
var connectionTask = Task.Run(() => ConnectionWatcher(bot, tokenSource.Token));
var disconnectTask = Task.Run(() => DisconnectTask(bot, tokenSource.Token));
var messageTask = Task.Run(() => MessageTask(bot, tokenSource.Token));
var replyTask = Task.Run(() => LoadReplies(replyResponder, tokenSource.Token));
Console.WriteLine("Connected... type 'close' to disconnect.");
try
{
await Task.WhenAll(connectionTask, consoleTask, messageTask, disconnectTask);
}
catch (TaskCanceledException) { }
}
private static async Task ConsoleTask(Bot bot, CancellationTokenSource tokenSource)
{
do
{
var message = Console.ReadLine();
switch (message)
{
case "close":
bot.Disconnect();
_running = false;
tokenSource.Cancel();
break;
case "channels":
Console.WriteLine($"Connected Channels:\r\n{string.Join("\r\n", bot.ConnectedChannels.Select(c => $"{c.Name}, ID: {c.ID}, Type: {c.Type}"))}");
break;
case "ping":
Console.WriteLine("Sending ping");
await bot.Shout("Pong");
break;
default:
break;
}
} while (_running);
}
private static async Task ConnectionWatcher(Bot bot, CancellationToken token)
{
do
{
if (_running && !bot.IsConnected)
{
Console.WriteLine("Connection lost, trying to reconnect...");
await bot.TryConnect(_token);
}
await Task.Delay(5000, token);
} while (_running);
}
private static async Task DisconnectTask(Bot bot, CancellationToken token)
{
do
{
await Task.Delay(TimeSpan.FromHours(1), token);
if (_running)
{
bot.Disconnect();
}
} while (_running);
}
private static async Task LoadReplies(WittyReplyResponder replyResponder, CancellationToken token)
{
do
{
var replyText = File.ReadAllText("replies.txt");
var replySplit = replyText.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
foreach (var replyPart in replySplit)
{
var replyParts = replyPart.Split(";");
var reply = replyParts[replyParts.Length - 1];
for (var i = 0; i < replyParts.Length - 1; i++)
{
var key = replyParts[i];
replyResponder.AddOrReplaceReply(key, reply);
}
}
await Task.Delay(TimeSpan.FromMinutes(5), token);
} while (_running);
}
private static async Task MessageTask(Bot bot, CancellationToken token)
{
var rnd = new Random();
do
{
if (IsWorkingHours())
{
var messages = File.ReadAllText("messages.txt");
var split = messages.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
var idx = rnd.Next(split.Length);
var message = split[idx];
await bot.Shout(message);
}
else
{
Console.WriteLine("Outside of working hours, bot is asleep...");
}
var rndSeconds = rnd.Next(1000);
var waitTime = TimeSpan.FromHours(3) + TimeSpan.FromSeconds(rndSeconds);
await Task.Delay(waitTime, token);
} while (_running);
}
private static bool IsWorkingHours() => IsWorkDay() && DateTime.Now.TimeOfDay.Between(TimeSpan.FromHours(8), TimeSpan.FromHours(18));
private static bool IsWorkDay() => DateTime.Now.DayOfWeek != DayOfWeek.Saturday && DateTime.Now.DayOfWeek != DayOfWeek.Sunday;
private static Bot CreateBot(WittyReplyResponder replyResponder)
{
var bot = new Bot();
var responders = new List<IResponder> {
replyResponder,
new HelloResponder(),
new GoogleMeetResponder(),
new ZoomResponder()
};
bot.Responders.AddRange(responders);
return bot;
}
}