forked from SingularBeing/PokebotStats
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
490 lines (436 loc) · 22.8 KB
/
Program.cs
File metadata and controls
490 lines (436 loc) · 22.8 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using System.Net;
using System.IO;
using System.Xml;
using System.Data;
using System.Runtime.InteropServices;
using MySql.Data.MySqlClient;
using System.Threading;
using System.Timers;
using System.Collections;
using Pokebot;
using GLaDOSbot;
namespace PokebotStats
{
class Program
{
/*
GLOBAL SET STRING VALUES
*/
string[] bannedRoles = new string[]
{
"Admin", "Mod", "DevGod", "Owner", "SubOwner", "BotGuy" , "AdminBot",
"@Admin", "@Mod", "@DevGod", "@Owner", "@SubOwner", "@BotGuy", "@AdminBot"
};
const string sql =
"Server=ultrarust.com;" +
"Database=singularbeing;" +
"Uid=singularbeing;" +
"Pwd=jXKcjyThJtBisZwW;";
string[] starterPokemon = new string[]
{
"Squirtle", "Charmander", "Bulbasaur"
};
/*
GLOBAL SET ULONG VALUES
*/
ulong tempServer = 203806251988025344;
System.Timers.Timer timer = new System.Timers.Timer();
static Program instance;
static void Main(string[] args)
=> new Program().Start();
private DiscordClient _client;
public class UserValues
{
public string name,nameId,money,wins,losses,pokemon1,pokemon2,pokemon3,pokemon4,pokemon5,pokemon6,starterpokemon;
public UserValues(string n, string n1, int n2, int n3, int n4, string n5, string n6, string n7, string n8, string n9, string n10, int n11)
{
name = n;
nameId = n1;
money = n2.ToString();
wins = n3.ToString();
losses = n4.ToString();
pokemon1 = n5;
pokemon2 = n6;
pokemon3 = n7;
pokemon4 = n8;
pokemon5 = n9;
pokemon6 = n10;
starterpokemon = n11.ToString();
}
}
public void Start()
{
instance = this;
timer.Elapsed += new ElapsedEventHandler(DisplayTimeEvent);
timer.Interval = 1000;
timer.Start();
_client = new DiscordClient();
_client.UsingCommands(x =>
{
x.PrefixChar = '!';
x.HelpMode = HelpMode.Public;
});
#region Commands
//pokemon stats
_client.GetService<CommandService>().CreateGroup("stats", x =>
{
_client.GetService<CommandService>().CreateCommand("all")
.Description("Shows the stats of all of a user's pokemon.")
.Do(async e =>
{
//check the database for this user
UserValues userVa = null;
string param = "SELECT * FROM users";
MySqlConnection con = null;
MySqlCommand cmd = null;
MySqlDataReader reader = null;
con = new MySqlConnection(sql);
cmd = new MySqlCommand(param, con);
new GLaDOSbot.Message("Starting connection.");
con.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
string aName = reader.GetString("name");
if (aName != null)
{
//check this for a name
if (aName == e.User.Name)
{
userVa = new UserValues(
reader.GetString("name"),
reader.GetString("nameId"),
reader.GetInt32("money"),
reader.GetInt32("wins"),
reader.GetInt32("losses"),
reader.GetString("pokemon1"),
reader.GetString("pokemon2"),
reader.GetString("pokemon3"),
reader.GetString("pokemon4"),
reader.GetString("pokemon5"),
reader.GetString("pokemon6"),
reader.GetInt32("starterpokemon")
);
new GLaDOSbot.Message($"Got the user {userVa.name}.");
}
}
}
con.Close();
con = null;
//we have the trainer, now we need to get the pokemon
string param2 = "SELECT * FROM userpokemon";
con = new MySqlConnection(sql);
cmd = new MySqlCommand(param2, con);
con.Open();
reader = cmd.ExecuteReader();
List<Pokemon> pokemon = new List<Pokemon>();
while (reader.Read())
{
pokemon.Add(new Pokemon(reader.GetString("name"), int.Parse(reader.GetString("userId")), reader.GetString("move1"), reader.GetString("move2"),
reader.GetString("move3"), reader.GetString("move4"), int.Parse(reader.GetString("exp")), reader.GetString("statusEffect"),
reader.GetString("heldItem"), int.Parse(reader.GetString("hp")), int.Parse(reader.GetString("attack")), int.Parse(reader.GetString("defense")),
int.Parse(reader.GetString("spatk")), int.Parse(reader.GetString("spdef")), int.Parse(reader.GetString("speed")), int.Parse(reader.GetString("level")), int.Parse(reader.GetString("nextlevel"))));
new GLaDOSbot.Message($"Adding pokemon {reader.GetString("name")}.");
}
con.Close();
con = null;
reader = null;
string finalString = string.Empty;
new GLaDOSbot.Message("Done with pokemon. Length: " + pokemon.Count);
bool hasPokemon = false;
bool pokemonExists = false;
foreach (Pokemon po in pokemon)
{
pokemonExists = true;
new GLaDOSbot.Message(po.name + "....yep");
new GLaDOSbot.Message("ID:" + userVa.nameId + ", " + po.userId);
if (po.userId == (int)e.User.Id && po.name == userVa.pokemon1)
{
hasPokemon = true;
//exists
await e.Channel.SendMessage(
$"__**{po.name}:**__" + '\n' +
$"**HP:** {po.hp}, **Attack:** {po.attack}" + '\n' +
$"**Defense:** {po.def}, **SpAtk:** {po.spatk}" + '\n' +
$"**SpDef:** {po.spdef}, **Speed:** {po.speed}" + '\n' +
$"**EXP:** {po.exp}, **Level:** {po.level}" + '\n' +
$"**EXP for Next Level:** {po.nextLevel}" + '\n' +
$"**Moves: {(string.IsNullOrEmpty(po.move1) ? "Unassigned" : po.move1)}, {(string.IsNullOrEmpty(po.move2) ? "Unassigned" : po.move2)}" + '\n' +
$"{(string.IsNullOrEmpty(po.move3) ? "Unassigned" : po.move3)}, {(string.IsNullOrEmpty(po.move4) ? "Unassigned" : po.move4)}");
}
else
{
new GLaDOSbot.Message("Pokemon does not exist. Adding.");
break;
}
}
if (!pokemonExists)
{
if (userVa.pokemon1 != null)
{
List<MySqlParameter> paramList = new List<MySqlParameter>();
paramList.Add(new MySqlParameter("name", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = userVa.pokemon1;
paramList.Add(new MySqlParameter("userId", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = (int)e.User.Id;
paramList.Add(new MySqlParameter("move1", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = "";
paramList.Add(new MySqlParameter("move2", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = "";
paramList.Add(new MySqlParameter("move3", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = "";
paramList.Add(new MySqlParameter("move4", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = "";
paramList.Add(new MySqlParameter("exp", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = 0;
paramList.Add(new MySqlParameter("statusEffect", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = "";
paramList.Add(new MySqlParameter("heldItem", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = "";
paramList.Add(new MySqlParameter("hp", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = 15;
paramList.Add(new MySqlParameter("attack", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = 0;
paramList.Add(new MySqlParameter("defense", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = 0;
paramList.Add(new MySqlParameter("spatk", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = 0;
paramList.Add(new MySqlParameter("spdef", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = 0;
paramList.Add(new MySqlParameter("speed", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = 0;
paramList.Add(new MySqlParameter("level", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = 5;
paramList.Add(new MySqlParameter("nextlevel", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = 85;
new GLaDOSbot.Message($"Adding pokemon {userVa.pokemon1}.");
SetValueInTable("userpokemon", "SELECT * FROM userpokemon", "name,userId,move1,move2,move3,move4,exp,statusEffect,heldItem,hp,attack,defense,spatk,spdef,speed,level,nextlevel", "?name,?userId,?move1,?move2,?move3,?move4,?exp,?statusEffect,?heldItem,?hp,?attack,?defense,?spatk,?spdef,?speed,?level,?nextlevel", paramList);
}
}
if (!hasPokemon)
{
if (userVa.pokemon1 != null)
{
new GLaDOSbot.Message("Adding pokemon 1.");
List<MySqlParameter> paramList = new List<MySqlParameter>();
paramList.Add(new MySqlParameter("name", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = userVa.pokemon1;
new GLaDOSbot.Message("0");
paramList.Add(new MySqlParameter("userId", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = (int)e.User.Id;
new GLaDOSbot.Message("0");
paramList.Add(new MySqlParameter("move1", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = "";
new GLaDOSbot.Message("0");
paramList.Add(new MySqlParameter("move2", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = "";
new GLaDOSbot.Message("0");
paramList.Add(new MySqlParameter("move3", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = "";
new GLaDOSbot.Message("0");
paramList.Add(new MySqlParameter("move4", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = "";
new GLaDOSbot.Message("0");
paramList.Add(new MySqlParameter("exp", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = 0;
new GLaDOSbot.Message("0");
paramList.Add(new MySqlParameter("statusEffect", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = "";
new GLaDOSbot.Message("0");
paramList.Add(new MySqlParameter("heldItem", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = "";
new GLaDOSbot.Message("0");
paramList.Add(new MySqlParameter("hp", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = 15;
new GLaDOSbot.Message("0");
paramList.Add(new MySqlParameter("attack", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = 0;
new GLaDOSbot.Message("0");
paramList.Add(new MySqlParameter("defense", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = 0;
new GLaDOSbot.Message("0");
paramList.Add(new MySqlParameter("spatk", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = 0;
new GLaDOSbot.Message("0");
paramList.Add(new MySqlParameter("spdef", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = 0;
new GLaDOSbot.Message("0");
paramList.Add(new MySqlParameter("speed", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = 0;
new GLaDOSbot.Message("0");
paramList.Add(new MySqlParameter("level", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = 5;
new GLaDOSbot.Message("0");
paramList.Add(new MySqlParameter("nextlevel", MySqlDbType.VarString));
paramList[paramList.Count - 1].Value = 85;
new GLaDOSbot.Message("0");
new GLaDOSbot.Message($"Adding pokemon {userVa.pokemon1}.");
SetValueInTable("userpokemon", "SELECT * FROM userpokemon", "name,userId,move1,move2,move3,move4,exp,statusEffect,heldItem,hp,attack,defense,spatk,spdef,speed,level,nextlevel", "?name,?userId,?move1,?move2,?move3,?move4,?exp,?statusEffect,?heldItem,?hp,?attack,?defense,?spatk,?spdef,?speed,?level,?nextlevel", paramList);
}
}
new GLaDOSbot.Message("After check.");
con.Close();
if (con != null) con = null;
if (reader != null) reader = null;
});
});
#endregion
_client.ExecuteAndWait(async () =>
{
new GLaDOSbot.Message("Pokebot-Stats v0.1 is now active and connected.");
await _client.Connect("MjAzOTY1MDIxOTc2Mzk1Nzc2.Cmwkug.U5-UNa-H7JirE2kFC0rPcMi6C1Y");
});
}
public static void DisplayTimeEvent(object source, ElapsedEventArgs e)
{
//updates every second
}
bool CheckUserInDatabase(string name, int id)
{
string param = "SELECT * FROM users";
MySqlConnection con = null;
MySqlCommand cmd = null;
MySqlDataReader reader = null;
List<PokeUser> users = new List<PokeUser>();
bool userExists = false;
bool exists = true;
try
{
con = new MySqlConnection(sql);
cmd = new MySqlCommand(param, con);
new GLaDOSbot.Message("Con is null: " + (con == null ? true : false).ToString());
con.Open();
reader = cmd.ExecuteReader();
new GLaDOSbot.Message("Reader is null: " + (reader == null ? true : false).ToString());
while (reader.Read())
{
new GLaDOSbot.Message("Reader is null: " + (reader == null ? true : false).ToString());
//users.Add(new PokeUser(reader.GetString("name"), reader.GetString("money")));
string aName = reader.GetString("name");
if (aName != null)
{
//check this for a name
if (aName == name)
{
userExists = true;
}
}
}
con.Close();
con = null;
con = new MySqlConnection(sql);
cmd = new MySqlCommand(param, con);
con.Open();
new GLaDOSbot.Message("Opened");
if (userExists)
{
new GLaDOSbot.Message($"User {name} exists!");
}
else
{
exists = false;
new GLaDOSbot.Message($"Setting new user {name}.");
//add to the database
cmd.CommandText = "INSERT INTO users(name,nameId,money,wins,losses,pokemon1,pokemon2,pokemon3,pokemon4,pokemon5,pokemon6,starterpokemon) VALUES(?name,?nameid,?money,?wins,?losses,?pokemon1,?pokemon2,?pokemon3,?pokemon4,?pokemon5,?pokemon6,?starterpokemon)";
cmd.Parameters.Add("?name", MySqlDbType.VarString).Value = name;
new GLaDOSbot.Message(id.ToString() + " = ID");
cmd.Parameters.Add("?nameId", MySqlDbType.VarString).Value = id.ToString();
cmd.Parameters.Add("?money", MySqlDbType.VarString).Value = "500";
cmd.Parameters.Add("?wins", MySqlDbType.VarString).Value = "0";
cmd.Parameters.Add("?losses", MySqlDbType.VarString).Value = "0";
cmd.Parameters.Add("?pokemon1", MySqlDbType.VarString).Value = "";
cmd.Parameters.Add("?pokemon2", MySqlDbType.VarString).Value = "";
cmd.Parameters.Add("?pokemon3", MySqlDbType.VarString).Value = "";
cmd.Parameters.Add("?pokemon4", MySqlDbType.VarString).Value = "";
cmd.Parameters.Add("?pokemon5", MySqlDbType.VarString).Value = "";
cmd.Parameters.Add("?pokemon6", MySqlDbType.VarString).Value = "";
cmd.Parameters.Add("?starterpokemon", MySqlDbType.VarString).Value = 0;
cmd.ExecuteNonQuery();
new GLaDOSbot.Message("Execute");
con.Close();
con = null;
con = new MySqlConnection(sql);
cmd = new MySqlCommand(param, con);
}
con.Open();
reader = cmd.ExecuteReader();
new GLaDOSbot.Message("Reader is null: " + (reader == null ? true : false).ToString());
while (reader.Read())
{
new GLaDOSbot.Message("Reader is null: " + (reader == null ? true : false).ToString());
//users.Add(new PokeUser(reader.GetString("name"), reader.GetString("money")));
string aName = reader.GetString("name");
if (aName != null)
{
//check this for a name
if (aName == name)
{
userExists = true;
}
}
}
if (userExists)
{
new GLaDOSbot.Message($"User {name} exists!");
}
else
{
new GLaDOSbot.Message($"User {name} does not exist.......wtf");
}
}
catch (Exception ex)
{
new GLaDOSbot.Message(string.Format("An error occurred {0}", ex.Message));
}
finally
{
if (reader != null) reader.Close();
if (con != null) con.Close();
new GLaDOSbot.Message("Done");
}
return exists;
}
private void SetValueInTable(string tableName, string param, string param1, string param2, List<MySqlParameter> parameters)
{
new GLaDOSbot.Message("Query" + '\n' + $"INSERT INTO {tableName}({param1}) VALUES({param2})");
new GLaDOSbot.Message("started");
MySqlConnection con = new MySqlConnection(sql);
MySqlCommand cmd = new MySqlCommand(param, con);
con.Open();
new GLaDOSbot.Message("Open");
cmd.CommandText = $"INSERT INTO {tableName}({param1}) VALUES({param2})";
foreach (MySqlParameter p in parameters)
{
new GLaDOSbot.Message("p=" + p.Value);
cmd.Parameters.Add(p);
}
new GLaDOSbot.Message(cmd.CommandText);
cmd.ExecuteNonQuery();
new GLaDOSbot.Message("execute");
con.Close();
new GLaDOSbot.Message("close");
con = null;
}
private struct InviteInfo
{
public string userId;
public string senderId;
public string server;
public string channel;
public InviteInfo(string p1, string p2, string p3, string p4)
{
userId = p1;
senderId = p2;
server = p3;
channel = p4;
}
}
}
}