-
Notifications
You must be signed in to change notification settings - Fork 0
/
MessageHandler.cs
200 lines (167 loc) · 6.25 KB
/
MessageHandler.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using Discord.WebSocket;
using System;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
namespace Sharpvin
{
internal class MessageHandler
{
//singleton instance
private static MessageHandler instance;
//fields
private FixedStringQueue MsgQueue;
private ulong selfID;
private readonly CurrencyConverter Currency;
private readonly MessageAnalysis Analysis;
private readonly YouTubeSearcher Youtube;
//constructor
private MessageHandler()
{
MsgQueue = FixedStringQueue.Create(3);
Currency = new CurrencyConverter();
Analysis = new MessageAnalysis();
Youtube = new YouTubeSearcher();
}
public void SetID(ulong id)
{
this.selfID = id;
}
public static MessageHandler GetInstance()
{
if (instance == null)
{
instance = new MessageHandler();
}
return instance;
}
public async void OnReady()
{
await Currency.UpdateRates();
}
public async Task OnMessageReceived(SocketMessage rawSM)
{
//ignore own messages and ignore Lenny commands
if (rawSM.Author.Id == instance.selfID || rawSM.Content.StartsWith('?'))
return;
//convert to lowercase
String msg = rawSM.Content.ToLower();
//spam aid
instance.MsgQueue.Push(msg);
if (instance.MsgQueue.isRepeating())
{
await rawSM.Channel.SendMessageAsync(rawSM.Content);
}
(bool isCommand, bool isPolite, bool hasNumber) = Analysis.ScanMessage(msg);
//autoconvert from czk to eur
if ((msg.Contains("czk") || msg.Contains("peanut")) && !isCommand)
{
String[] words = msg.Split(' ');
double value = 0;
bool isNumeric = false;
foreach (String word in words)
{
String w = word.Replace('.', ',');
isNumeric = double.TryParse(w, out value);
if (isNumeric)
break;
}
if (isNumeric)
{
double eur = Currency.Convert(value, "CZK", "EUR");
await rawSM.Channel.SendMessageAsync($"{value.ToString().Replace(',', '.')} peanuts is {eur.ToString().Replace(',', '.')} EUR");
}
}
if (isCommand)
{
//convert currency
if (msg.Contains("convert"))
{
if (!isPolite)
{
await rawSM.Channel.SendMessageAsync("Do it yourself");
return;
}
else if (!hasNumber)
{
await rawSM.Channel.SendMessageAsync("I don't see a number to convert");
return;
}
double value = 0, final = 0;
String code_from = "", code_to = "";
try
{
(value, code_from, code_to) = Analysis.FindConversion(msg);
final = Currency.Convert(value, code_from, code_to);
}
catch (Exception ex)
{
await rawSM.Channel.SendMessageAsync(ex.Message);
return;
}
await rawSM.Channel.SendMessageAsync($"{value.ToString().Replace(',', '.')} {code_from} is {final.ToString().Replace(',', '.')} {code_to}");
}
//randomly generated answers
else if (msg.Contains('?'))
{
Random random = new Random();
if (msg.Contains("or"))
{
List<String> options = Analysis.FindOptions(msg);
int index = random.Next(options.Count);
await rawSM.Channel.SendMessageAsync(options[index]);
}
else
{
List<String> answers = new List<String>()
{
"yeah",
"yes",
"yup",
"nope",
"no",
"nah"
};
int index = random.Next(answers.Count);
await rawSM.Channel.SendMessageAsync(answers[index]);
}
}
//fetching youtube videos
else if (msg.Contains("youtube"))
{
if (!isPolite)
{
await rawSM.Channel.SendMessageAsync("Do it yourself");
return;
}
String query = Analysis.FindQuery(msg);
if (query != "")
{
String video = await Youtube.Search(query);
await rawSM.Channel.SendMessageAsync(video);
}
else
{
await rawSM.Channel.SendMessageAsync("I didn't understand the request, sorry");
}
}
else if (msg.Contains("next video"))
{
if (!isPolite)
{
await rawSM.Channel.SendMessageAsync("Do it yourself");
return;
}
String video = Youtube.NextVideo();
if (video != "")
await rawSM.Channel.SendMessageAsync(video);
else
await rawSM.Channel.SendMessageAsync("Nothing left to post");
}
}
}
}
}