DeukBot4/DeukBot4/Program.cs

89 lines
3.0 KiB
C#
Raw Normal View History

2018-03-28 23:34:48 +00:00
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
2018-03-28 23:34:48 +00:00
using System.Threading.Tasks;
using DeukBot4.Database;
using DeukBot4.Database.ServerSettings;
2018-03-28 23:34:48 +00:00
using DeukBot4.MessageHandlers;
using DeukBot4.MessageHandlers.CommandHandler;
2018-10-08 17:51:19 +00:00
using DeukBot4.MessageHandlers.JokeHandling;
2018-03-28 23:34:48 +00:00
using Discord;
using Discord.WebSocket;
2018-08-13 17:26:11 +00:00
using ReminderHandler = DeukBot4.Database.ReminderHandler;
2018-03-28 23:34:48 +00:00
namespace DeukBot4
{
internal static class Program
2018-03-28 23:34:48 +00:00
{
public static DiscordSocketClient Client { get; private set; }
public static Settings Settings { get; private set; }
public static ulong BotId { get; private set; }
2018-10-08 16:36:22 +00:00
public static bool IsConnected { get; private set; }
public static string Version { get; private set; }
2018-03-28 23:34:48 +00:00
private static void Main(string[] args)
{
MainAsync().GetAwaiter().GetResult();
}
2018-08-13 16:41:59 +00:00
private static async Task SetupScheduler()
{
ReminderHandler.Main.CheckReminders();
BirthdayHandler.Initialize();
2018-10-08 16:36:22 +00:00
BirthdayHandler.CheckBirthdays();
2018-08-13 16:41:59 +00:00
}
2018-03-28 23:34:48 +00:00
private static async Task MainAsync()
{
var assembly = Assembly.GetExecutingAssembly();
var attr = assembly.GetCustomAttributes<AssemblyMetadataAttribute>()
.FirstOrDefault(x => string.Equals(x.Key, "GitHash", StringComparison.InvariantCultureIgnoreCase));
Version = attr.Value;
Console.WriteLine(Version);
2018-08-13 16:41:59 +00:00
2018-03-28 23:34:48 +00:00
Settings = Settings.FromJsonFile("settings.json");
DatabaseConnection.ConnectionString = Settings.DatabaseConnectionString;
2018-08-13 16:41:59 +00:00
await SetupScheduler();
2018-10-08 17:51:19 +00:00
JokeHandler.Initialize();
DatabaseInitializer.Initialize();
ServerSettingHandler.OnBotStartUp();
2018-07-30 20:44:45 +00:00
TagStorage.BuildTags();
2018-03-28 23:34:48 +00:00
CommandHandler.Build();
2018-04-03 12:40:24 +00:00
ImageBackupHandler.SetBackupChannels(Settings.BackupChannels);
ImageBackupHandler.Settings = Settings.WebDavSettings;
2018-04-03 12:40:24 +00:00
2018-03-28 23:34:48 +00:00
Client = new DiscordSocketClient();
2018-08-13 16:41:59 +00:00
Client.Log += Logger.Main.LogDiscord;
2018-03-28 23:34:48 +00:00
Client.Ready += OnReady;
Client.MessageReceived += MainHandler.HandleMessage;
await Client.LoginAsync(TokenType.Bot, Settings.Token);
await Client.StartAsync();
// Block this task until the program is closed.
await Task.Delay(-1);
}
private static async Task OnReady()
{
2018-07-30 20:44:45 +00:00
Client.CurrentUser.ModifyAsync(properties =>
2018-03-28 23:34:48 +00:00
{
properties.Username = Settings.Username;
2018-04-21 21:01:44 +00:00
properties.Avatar = new Image("avatar.png");
2018-03-28 23:34:48 +00:00
});
EmbedFactory.Initialize(Client.CurrentUser);
2018-07-30 20:44:45 +00:00
Console.WriteLine(("Joined Guilds: "));
foreach (var clientGuild in Client.Guilds)
{
Console.WriteLine($"- {clientGuild.Id}: {clientGuild.Name}");
}
BotId = Client.CurrentUser.Id;
2018-10-08 16:36:22 +00:00
IsConnected = true;
2018-03-28 23:34:48 +00:00
}
}
}