Files
Sean/Sean/Program.cs

83 lines
2.7 KiB
C#
Raw Permalink Normal View History

2019-05-22 21:38:20 +02:00
using Discord;
using Discord.Commands;
2019-05-22 21:38:20 +02:00
using Discord.WebSocket;
using Microsoft.Extensions.DependencyInjection;
using Sean.Services;
using Sentry;
2019-05-22 21:38:20 +02:00
using System;
using System.Threading.Tasks;
namespace Sean
{
internal class Program
{
#region Fields
private DiscordSocketClient client;
private CommandHandler commandHandler;
private CommandService commandService;
private CommandServiceConfig commandServiceConfig;
2019-05-22 21:38:20 +02:00
#endregion Fields
#region Methods
//.AddSingleton<DatabaseService>()
public IServiceProvider BuildProvider() => new ServiceCollection().BuildServiceProvider();
public IServiceProvider BuildServiceProvider() => new ServiceCollection()
.AddSingleton(client)
.AddSingleton(commandService)
.AddSingleton<CommandHandler>()
.AddSingleton<DbService>()
.BuildServiceProvider();
2019-05-22 21:38:20 +02:00
public async Task MainAsync()
{
commandServiceConfig = new CommandServiceConfig
{
CaseSensitiveCommands = false,
LogLevel = LogSeverity.Debug
};
2019-05-22 21:38:20 +02:00
commandService = new CommandService(commandServiceConfig);
client = new DiscordSocketClient(new DiscordSocketConfig { MessageCacheSize = 100, AlwaysDownloadUsers = true/*, LogLevel = LogSeverity.Debug */});
client.Log += Log;
IServiceProvider provider = BuildServiceProvider();
commandHandler = new CommandHandler(provider, client, commandService);
await commandHandler.InstallCommandsAsync();
2019-05-22 21:38:20 +02:00
await client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("DiscordToken_Sean"));
await client.StartAsync();
2019-05-22 21:38:20 +02:00
//client.Disconnected += Client_Disconnected;
2019-05-22 21:38:20 +02:00
// Block this task until the program is closed.
await Task.Delay(-1);
}
//private Task Client_Disconnected(Exception arg)
//{
// Environment.Exit(0);
// return Task.CompletedTask;
//}
2019-05-22 21:38:20 +02:00
private static void Main(string[] args)
{
using (SentrySdk.Init(Environment.GetEnvironmentVariable("SentryURL_Sean")))
{
try
{
new Program().MainAsync().GetAwaiter().GetResult();
}
catch (Exception e)
{
SentrySdk.CaptureException(e);
}
}
}
2019-05-22 21:38:20 +02:00
private Task Log(LogMessage msg)
{
Console.WriteLine(msg.ToString());
return Task.CompletedTask;
}
#endregion Methods
}
}