This repository was archived by the owner on May 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
232 lines (206 loc) · 8.49 KB
/
Copy pathProgram.cs
File metadata and controls
232 lines (206 loc) · 8.49 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
using System;
using System.Data;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Discord;
using Discord.WebSocket;
using MySql.Data;
using MySql.Data.MySqlClient;
namespace csharp
{
enum RestartOptions
{
exit = 88,
restart = 42
}
public class Program
{
private static DiscordSocketClient _client;
public static MySqlConnection conn;
private const string connStr = "server=spartan364.hopto.org;user=Cougartalk;database=BnBData;port=3306;password=";
private static bool sentStartupMessage = false;
private Dictionary<string, Func<SocketMessage, string[], Task>> commands;
public string HelpText { get; private set; }
public static void Main(string[] args)
=> new Program().MainAsync(args).GetAwaiter().GetResult();
public async Task MainAsync(string[] args)
{
_client = new DiscordSocketClient();
_client.Log += Log;
//conn = new MySqlConnection(connStr + config.instance.DBPass);
//await conn.OpenAsync();
//Console.WriteLine(conn.Ping());
//await conn.CloseAsync();
//await Library.instance.loadChips();
//await NCPLibrary.instance.loadNCPs();
//var tasks = ReloadData();
//await Task.WhenAll(tasks);
await ReloadData();
_client.MessageReceived += MessageRecieved;
_client.Ready += BotReady;
commands = new Dictionary<string, Func<SocketMessage, string[], Task>>
{
{ "die", ExitCheck },
{ "chip", Library.instance.SendChip },
{ "ncp", NCPLibrary.instance.SendNCP },
{ "skill", Library.instance.SearchBySkill },
{ "element", Library.instance.SearchByElement },
{ "skilluser", Library.instance.SearchBySkillUser },
{ "skilltarget", Library.instance.SearchBySkillTarget },
{ "skillcheck", Library.instance.SearchBySkillCheck },
{ "reload", ReloadData },
{ "roll", Dice.instance.RollDice },
{ "rollstats", Dice.instance.RollStats },
{ "restart", ExitCheck },
{ "virus", VirusCompendium.instance.SendVirus },
{ "cr", VirusCompendium.instance.SendCR },
{ "encounter", VirusCompendium.instance.RandomEncounter },
{ "viruselement", VirusCompendium.instance.SendVirusElements },
{ "help", SendHelpMessage }
//{ "join", BotMusic.Instance.JoinVoiceChannel },
//{ "leave", BotMusic.Instance.LeaveVoiceChannel },
//{ "play", BotMusic.Instance.PlayMusic }
};
//await ChipImages.Instance.loadChipImages();
await _client.LoginAsync(TokenType.Bot, config.instance.Token);
await _client.StartAsync();
await Task.Delay(-1);
}
public static Task Log(LogMessage msg)
{
Console.WriteLine(msg.ToString());
return Task.CompletedTask;
}
private async Task BotReady()
{
await _client.SetGameAsync("%help for a list of commands");
if (sentStartupMessage)
{
return;
}
this.HelpText = await File.ReadAllTextAsync("./help.txt");
var major = _client.GetUser(config.instance.MajorIDConverted);
if (major == null)
{
Console.WriteLine("could not fetch Major");
return;
}
string message;
if (Environment.GetCommandLineArgs().Length == 1)
{
message = "Started with no commandLine args";
}
else
{
message = string.Format("Started with {0} as the restart code", Environment.GetCommandLineArgs()[1]);
}
await major.SendMessageAsync(message);
sentStartupMessage = true;
}
private async Task MessageRecieved(SocketMessage message)
{
if (message.Author.IsBot) return;
if (!message.Content.Trim().StartsWith(config.instance.Prefix))
{
return;
}
var args = message.Content.Split(" ");
args[0] = args[0].Substring(config.instance.Prefix.Length).ToLower(); //remove starting command prefix
//Console.WriteLine(args[0]);
if (args[0] == string.Empty)
{
return;
}
if (!commands.TryGetValue(args[0], out var func))
{
await Library.instance.SendChip(message, args);
return;
}
_ = Task.Run(async () =>
{
try
{
await func.Invoke(message, args);
}
catch (Exception e)
{
#if DEBUG
await message.Channel.SendMessageAsync(e.Message);
Console.WriteLine(e.Message);
#else
await Log(new Discord.LogMessage(LogSeverity.Error, "command", e.Message, e));
#endif
}
});
}
private async Task ExitCheck(SocketMessage message, string[] args)
{
if (message.Author.Id != config.instance.MajorIDConverted && message.Author.Id != config.instance.JinIDConverted)
{
await message.Channel.SendMessageAsync("You do not have permission to do this, if this is in error, inform Major");
return;
}
BotMusic.Instance.Dispose();
Dice.instance.Dispose();
conn?.Dispose();
if (args[0].ToLower() == "restart")
{
await _client.SetStatusAsync(UserStatus.Invisible);
Environment.Exit((int)RestartOptions.restart);
}
else
{
await _client.SetStatusAsync(UserStatus.Invisible);
Environment.Exit((int)RestartOptions.exit);
}
}
private async Task SendHelpMessage(SocketMessage message, string[] args = null) =>
await message.Author.SendMessageAsync(this.HelpText);
private Task ReloadData(SocketMessage message = null, string[] args = null)
{
if (message != null && message.Author.Id != config.instance.JinIDConverted
&& message.Author.Id != config.instance.MajorIDConverted)
{
message.Author.SendMessageAsync("You do not have permission to do this, if you think this is in error, inform Major.");
return null;
}
Task[] toReturn = new Task[3];
toReturn[0] = Task.Run(async () =>
{
try
{
await Library.instance.LoadChips(message);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
});
toReturn[1] = Task.Run(async () =>
{
try
{
await NCPLibrary.instance.LoadNCPs(message);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
});
toReturn[2] = Task.Run(async () =>
{
try
{
await VirusCompendium.instance.LoadViruses(message);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
});
return Task.WhenAll(toReturn);
}
}
}