-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyBot.cs
More file actions
266 lines (244 loc) · 10.2 KB
/
MyBot.cs
File metadata and controls
266 lines (244 loc) · 10.2 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
using Discord;
using Discord.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SimpleBot
{
class MyBot
{
DiscordClient discord;
public MyBot()
{
var rnd = new Random();
discord = new DiscordClient();
discord.UsingCommands(x =>
{
x.PrefixChar = '!';
x.AllowMentionPrefix = true;
});
// -- COMMANDS --
var commands = discord.GetService<CommandService>();
// HELLO : greets user in the same language
String[] engHiArr = new String[] { "Hi", "Hello", "Hey", "Howdy", "Yo", "Sup", "Hiya", "'ello", "Greetings" };
String[] japHiArr = new String[] { "Konnichiwa", "Ohayō gozaimasu", "Ohayo gozaimasu", "Ohayō", "Ohayo" };
String[] polHiArr = new String[] { "Cześć", "Czesc", "Dzień dobry", "Dzien dobry" };
commands.CreateCommand("Hi")
.Alias(engHiArr.Concat(japHiArr).Concat(polHiArr).ToArray())
.Do(async (e) =>
{
var greeting = "Hi";
//find language bot was greeted in, and choose random greeting from matching language
if (engHiArr.Contains(e.Message.Text.Substring(1), StringComparer.OrdinalIgnoreCase))
{
greeting = engHiArr.ElementAt((rnd.Next(0, engHiArr.Length)));
}
else if (japHiArr.Contains(e.Message.Text.Substring(1), StringComparer.OrdinalIgnoreCase))
{
greeting = japHiArr.ElementAt((rnd.Next(0, japHiArr.Length)));
}
else if (polHiArr.Contains(e.Message.Text.Substring(1), StringComparer.OrdinalIgnoreCase))
{
greeting = polHiArr.ElementAt((rnd.Next(0, polHiArr.Length)));
}
await e.Channel.SendMessage(greeting + " " + e.User.Name);
});
// RANDUSER : choose random user; if given message, display message after user (i.e. randomUser "won a prize!")
commands.CreateCommand("randUser")
.Alias("ru", "randomuser", "ruser")
.Parameter("message", ParameterType.Unparsed)
.Do(async (e) =>
{
var userList = e.Channel.Users;
User randomUser = userList.ElementAt((rnd.Next(0, userList.Count())));
if (e.GetArg("message") == "") //default, no message given
{
await e.Channel.SendMessage(e.User.Name + " randomly chose... " + randomUser.Mention);
}
else //give user and message
{
await e.Channel.SendMessage(randomUser.Mention + " " + String.Join(String.Empty, e.Args.ToArray()));
}
});
// FLIPCOIN : flip a coin and show result (heads or tails)
commands.CreateCommand("flipCoin")
.Alias("fc", "flip", "coin", "coinflip", "cf")
.Do(async (e) =>
{
int curNum = (new Random().Next(0, 2)); //choose either 0 or 1
String flippedCoin = "Heads";
if (curNum == 1)
{
flippedCoin = "Tails";
}
await e.Channel.SendMessage(e.User.Name + " flipped a coin: " + flippedCoin);
});
// ROLL : roll dice; supports rolling any sided die and any number of dice, along with ability to combine with numbers
//ex: !r d4 (roll one four-sided dice), !r 2d10 (roll two d10), !r 15d2 + 3 (roll fifteen d2 and add 3 to the result)
//combines input left to right, i.e. 2 + 3 * 4 is ((2 + 3) * 4)
commands.CreateCommand("roll")
.Alias("r", "dice")
.Parameter("dice", ParameterType.Multiple)
.Do(async (e) =>
{
int total = 0;
if (validStmt(e.Args, ref total))
{
await e.Channel.SendMessage(e.User.Mention + " rolled " + total);
}
else
{
await e.Channel.SendMessage("Sorry " + e.User.Mention + "! I didn't understand your roll.");
}
});
//returns true if given args make up a valid roll statement
//puts roll result into total
bool validStmt(string[] args, ref int total)
{
char prevOp = '+';
if (args.Length % 2 != 0) //need odd number of arguments (!r "d2" "+" "5", not !r "2d6" "+")
{
for (int i = 0; i < args.Length; i++)
{
String curArg = args.ElementAt(i);
if (i % 2 == 0) //even arguments should be dice
{
if (validDice(curArg))
{
int rRes = rollResult(curArg);
if (rRes != -1) {
total = execOp(total, rRes, prevOp);
}
else
{
return false;
}
}
else
{
return false;
}
}
else //odd arguments should be operators
{
if (validOperator(curArg))
{
prevOp = curArg[0];
}
else
{
return false;
}
}
}
return true;
}
return false;
}
//returns true if given string is a valid dice argument
bool validDice(String diceStr)
{
if (diceStr.All(Char.IsDigit)) //just a number ("5" or "100")
{
return true;
}
int dIndex = diceStr.IndexOf('d'); //find first 'd' char, if there is one
if (dIndex != -1 && dIndex != diceStr.Length - 1) //diceStr contains 'd' and that 'd' isn't the last char
{
var diceStrNoD = diceStr.Where((v, i) => i != dIndex).ToList(); //remove that 'd' from diceStr
if (diceStrNoD.Count >= 1 && diceStrNoD.All(Char.IsDigit)) //there are remaining chars, and all of them are digits
{
return true;
}
}
return false;
}
//returns the result of rolling the dice specified in diceStr
int rollResult(String diceStr)
{
int x = 0; //used for TryParse
int numDice = 1;
int dIndex = diceStr.IndexOf('d'); //find first 'd' char
if (diceStr.All(Char.IsDigit)) //just a number ("5" or "100")
{
return Int32.TryParse(diceStr, out x) ? x : -1;
}
else if (diceStr[0] != 'd') //multiple dice i.e. "2d6"
{
String numDiceStr = diceStr.Substring(0, dIndex);
numDice = Int32.TryParse(numDiceStr, out x) ? x : -1;
if (numDice == -1) //parse failed
{
return -1;
}
}
//roll typeDice (d6,d8,etc) numDice times, store in retVal
int retVal = 0;
String typeDiceStr = diceStr.Substring(dIndex + 1);
int typeDice = Int32.TryParse(typeDiceStr, out x) ? x : -1;
if (typeDice == -1) //parse failed
{
return -1;
}
for (int j = 0; j < numDice; j++)
{
try
{
retVal = checked (retVal + rollDice(typeDice));
}
catch (OverflowException) //retVal too large, just return maxValue int
{
return int.MaxValue;
}
}
return retVal;
}
//roll a die, choosing a number between 1 and maxVal (including)
int rollDice(int maxVal)
{
return (rnd.Next(1, maxVal + 1));
}
//returns true if opStr is a valid operator
bool validOperator(String opStr)
{
return opStr.Length == 1 && opStr.IndexOfAny("+-*/%^".ToCharArray()) != -1;
}
//return combination of num1 and num2 using given operation
//if no operation matches, returns -1
int execOp(int num1, int num2, char op)
{
if (op == '+')
{
return num1 + num2;
}
else if (op == '-')
{
return num1 - num2;
}
else if (op == '*')
{
return num1 * num2;
}
else if (op == '/')
{
return num1 / num2;
}
else if (op == '%')
{
return num1 % num2;
}
else if (op == '^')
{
return (int)Math.Pow(num1, num2);
}
return -1;
}
discord.ExecuteAndWait(async () =>
{
await discord.Connect({bot-token}, TokenType.Bot);
});
}
}
}