-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBot.cs
152 lines (137 loc) · 5.67 KB
/
Bot.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
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.EventArgs;
using RoxasBot.Services;
using System;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
namespace RoxasBot
{
public class Bot : IDisposable
{
Config _config;
DiscordClient _client;
public readonly CancellationTokenSource _cts;
private CommandsNextExtension _cnext;
public Bot()
{
if (Config.UseFile)
{
if (!File.Exists("config.json"))
{
new Config().SaveToFile("config.json");
#region !! Report to user that config has not been set yet !! (aesthetics)
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.Black;
WriteCenter("▒▒▒▒▒▒▒▒▒▄▄▄▄▒▒▒▒▒▒▒", 2);
WriteCenter("▒▒▒▒▒▒▄▀▀▓▓▓▀█▒▒▒▒▒▒");
WriteCenter("▒▒▒▒▄▀▓▓▄██████▄▒▒▒▒");
WriteCenter("▒▒▒▄█▄█▀░░▄░▄░█▀▒▒▒▒");
WriteCenter("▒▒▄▀░██▄░░▀░▀░▀▄▒▒▒▒");
WriteCenter("▒▒▀▄░░▀░▄█▄▄░░▄█▄▒▒▒");
WriteCenter("▒▒▒▒▀█▄▄░░▀▀▀█▀▒▒▒▒▒");
WriteCenter("▒▒▒▄▀▓▓▓▀██▀▀█▄▀▀▄▒▒");
WriteCenter("▒▒█▓▓▄▀▀▀▄█▄▓▓▀█░█▒▒");
WriteCenter("▒▒▀▄█░░░░░█▀▀▄▄▀█▒▒▒");
WriteCenter("▒▒▒▄▀▀▄▄▄██▄▄█▀▓▓█▒▒");
WriteCenter("▒▒█▀▓█████████▓▓▓█▒▒");
WriteCenter("▒▒█▓▓██▀▀▀▒▒▒▀▄▄█▀▒▒");
WriteCenter("▒▒▒▀▀▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒");
Console.BackgroundColor = ConsoleColor.Yellow;
WriteCenter("WARNING", 3);
Console.ResetColor();
WriteCenter("Thank you Mario!", 1);
WriteCenter("But our config.json is in another castle!");
WriteCenter("(Please fill in the config.json that was generated.)", 2);
WriteCenter("Press any key to exit..", 1);
Console.SetCursorPosition(0, 0);
Console.ReadKey();
#endregion
Environment.Exit(0);
}
_config = Config.LoadFromFile("config.json");
}
else
{
_config = Config.LoadFromCS();
}
#region !! Welcome Message !! (aesthetics)
Console.ForegroundColor = ConsoleColor.DarkCyan;
WriteCenter(@"Booting Roxas Bot");
WriteCenter();
Console.ResetColor();
#endregion
_client = new DiscordClient(new DiscordConfiguration()
{
TokenType = TokenType.Bot,
AutoReconnect = true,
Token = _config.Token,
LogLevel = DSharpPlus.LogLevel.Debug,
UseInternalLogHandler = false,
});
_cnext = _client.UseCommandsNext(new CommandsNextConfiguration()
{
CaseSensitive = false,
EnableDefaultHelp = true,
EnableMentionPrefix = true,
StringPrefixes = new[] { _config.Prefix },
IgnoreExtraArguments = true
});
RegisterCommands();
_cts = new CancellationTokenSource();
//Keep this bit at the end of the CTOR
_client.Ready += OnReadyAsync;
}
internal void WriteCenter(string value = "", int skipline = 0)
{
for (int i = 0; i < skipline; i++)
{
Console.WriteLine();
}
try
{
Console.SetCursorPosition((Console.WindowWidth - value.Length) / 2, Console.CursorTop);
}
catch
{
}
Console.WriteLine(value);
}
public async Task RunAsync()
{
await _client.ConnectAsync();
await WaitForCancellationAsync();
}
private async Task WaitForCancellationAsync()
{
while (!_cts.IsCancellationRequested)
{
await Task.Delay(500);
}
}
public void Dispose()
{
}
private async Task OnReadyAsync(ReadyEventArgs e)
{
_client.DebugLogger.LogMessage(DSharpPlus.LogLevel.Info, "RoxasBot", "RoxasBot Finished Booting!", DateTime.Now);
await Task.Yield();
}
private void RegisterCommands()
{
_cnext.RegisterCommands(Assembly.GetExecutingAssembly());
Console.ForegroundColor = ConsoleColor.DarkYellow;
WriteCenter("Loaded commands:");
Console.ResetColor();
WriteCenter("-----");
foreach (System.Collections.Generic.KeyValuePair<string, Command> command in _cnext.RegisteredCommands)
{
WriteCenter(_config.Prefix + command.Key + $" {(command.Value.IsHidden ? "(Hidden)" : "")}");
}
WriteCenter("-----");
WriteCenter();
}
}
}