-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
163 lines (132 loc) · 5.42 KB
/
Program.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
using System;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Linq;
using System.Collections.Concurrent;
using Discord.Audio;
using System.Threading;
namespace Sevenisko.IceBot
{
class Program
{
static DiscordSocketClient client;
public static ConfigFile config;
Admin.WebServer webServer;
[DllImport("Kernel32")]
public static extern bool SetConsoleCtrlHandler(EventHandler Handler, bool Add);
// An enumerated type for the control messages
// sent to the handler routine.
public enum CtrlType
{
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT,
CTRL_CLOSE_EVENT,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT
}
public delegate bool EventHandler(CtrlType sig);
private EventHandler _handler;
private static bool ConsoleCtrlCheck(CtrlType ctrlType)
{
LogText(LogSeverity.Info, "Main", "Exiting IceBot...");
client.LogoutAsync();
client.StopAsync();
LogText(LogSeverity.Info, "Main", "IceBot successfully exited, have i nice day and goodbye.");
Environment.Exit(0);
return true;
}
public static void ThrowException(object sender, UnhandledExceptionEventArgs e)
{
Console.WriteLine("*** IceBot has stopped working properly! ***");
Console.WriteLine("Exception class: " + (e.ExceptionObject as Exception).GetType().Name);
Console.WriteLine("Source: " + (e.ExceptionObject as Exception).Source);
Console.WriteLine("Message: " + (e.ExceptionObject as Exception).Message);
Console.WriteLine("Stack trace: \n" + (e.ExceptionObject as Exception).StackTrace);
Environment.Exit(1);
}
public static void ThrowFatal(string message)
{
LogText(LogSeverity.Critical, "Main", message);
Environment.Exit(1);
}
public static void Main(string[] args)
=> new Program().StartBot().GetAwaiter().GetResult();
public async Task StartBot()
{
Console.Title = "IceBot v" + BotInfo.GetBotVersion();
Console.WriteLine("============================");
Console.WriteLine(" ...IceBot is starting... ");
Console.WriteLine("============================");
_handler = new EventHandler(ConsoleCtrlCheck);
SetConsoleCtrlHandler(_handler, true);
AppDomain.CurrentDomain.UnhandledException += ThrowException;
config = Configuration.LoadConfig("Config.xml");
client = new DiscordSocketClient();
webServer = new Admin.WebServer();
if (config.BotToken == "InsertHere")
{
ThrowFatal("Set your bot token first!");
}
CommandServiceConfig servConf = new CommandServiceConfig();
servConf.CaseSensitiveCommands = true;
servConf.DefaultRunMode = RunMode.Async;
servConf.IgnoreExtraArgs = false;
CommandService service = new CommandService(servConf);
Services.CommandHandler handler = new Services.CommandHandler(client, service);
await handler.InstallCommandsAsync();
client.Log += Log;
client.Ready += Client_Ready;
try
{
await client.LoginAsync(TokenType.Bot, config.BotToken);
}
catch
{
Environment.Exit(1);
}
await client.StartAsync();
if (config.WebSettings.Enabled)
{
webServer.Start($"http://{config.WebSettings.Address}:{config.WebSettings.Port}/");
}
await Task.Delay(-1);
}
private async Task Client_Ready()
{
await LogText(LogSeverity.Info, "Main", "Connected as " + client.CurrentUser.Username + "#" + client.CurrentUser.Discriminator);
Shared.discordServer = client.Guilds.First();
await client.SetGameAsync(config.StatusOnJoin, null, ActivityType.Playing);
}
public static Task LogText(LogSeverity severity, string source, string message)
{
Log(new LogMessage(severity, source, message));
return Task.CompletedTask;
}
public static Task Log(LogMessage msg)
{
Console.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss") + " - " + msg.Source + "]: " + msg.Message);
return Task.CompletedTask;
}
}
public class AdminUser
{
public string Username;
public string Password;
}
public class Shared
{
public static List<string> BadWords { get; set; }
public static bool commandsEnabled = true;
public static SocketGuild discordServer { get; set; }
public static ConcurrentDictionary<ulong, IAudioClient> ConnectedChannels = new ConcurrentDictionary<ulong, IAudioClient>();
public static ConcurrentQueue<AudioFile> Playlist = new ConcurrentQueue<AudioFile>();
public static AudioDownloader AudioDownloader = new AudioDownloader();
public static AudioPlayer AudioPlayer = new AudioPlayer();
public static bool AutoPlay;
public static bool AutoPlayRunning;
}
}