public async Task Start() { DiscordClient = new DiscordShardedClient(new DiscordSocketConfig() { LogLevel = LogSeverity.Verbose, AlwaysDownloadUsers = true, MessageCacheSize = 100, }); DiscordClient.Log += Logger.LogClientMessage; DiscordClient.GuildAvailable += OnGuildConnected; try { await DiscordClient.LoginAsync(TokenType.Bot, GlobalConfiguration.Token); await DiscordClient.StartAsync(); } catch (Exception e) { await Logger.LogErrorMessage($"{e.Message}\n{e.StackTrace.Split('\n').First(s => s.Contains("line"))}"); return; } if (GlobalConfiguration.OwnerId == 0) { GlobalConfiguration.OwnerId = DiscordClient.GetApplicationInfoAsync().Result.Owner.Id; GlobalConfiguration.Save(); } foreach (var shard in DiscordClient.Shards) { shard.Ready += OnReady; shard.SetGameAsync(GlobalConfiguration.PlayingStatus).FireAndForget(); } DiscordClient.MessageReceived += MessageEventHandler.MessageRecieved; DiscordClient.MessageDeleted += MessageEventHandler.MessageDeleted; DiscordClient.JoinedGuild += GuildEventHandler.OnJoinedGuild; DiscordClient.LeftGuild += GuildEventHandler.OnLeftGuild; DiscordClient.UserLeft += UserEventHandler.UserLeft; var serviceProvider = ConfigureServices(); var _handler = new CommandHandler(); await _handler.Install(serviceProvider); Updater.Start(); // Block this program until it is closed. await Task.Delay(-1); }
public async Task Start() { DiscordClient = new DiscordShardedClient(new DiscordSocketConfig() { LogLevel = LogSeverity.Verbose, AlwaysDownloadUsers = true, MessageCacheSize = 100, }); DiscordClient.Log += Logger.LogClientMessage; DiscordClient.GuildAvailable += OnGuildConnected; try { await DiscordClient.LoginAsync(TokenType.Bot, GlobalConfiguration.Token); await DiscordClient.StartAsync(); } catch (Exception e) { await Logger.LogErrorMessage($"{e.Message}\n{e.StackTrace.Split('\n').First(s => s.Contains("line"))}"); return; } if (GlobalConfiguration.OwnerId == 0) { GlobalConfiguration.OwnerId = DiscordClient.GetApplicationInfoAsync().Result.Owner.Id; GlobalConfiguration.Save(); } foreach (var shard in DiscordClient.Shards) { shard.Ready += OnReady; shard.SetGameAsync(GlobalConfiguration.PlayingStatus).FireAndForget(); shard.Disconnected += async(exception) => { Console.WriteLine($"{exception.Message}\n{exception.StackTrace}"); string dcInfo = $"`{DateTime.Now}`: Disconnected\n"; if (LastCommand.Message.CreatedAt - DateTimeOffset.Now < TimeSpan.FromSeconds(15)) { dcInfo += $"**(Possibly Related)**\n"; } var msg = LastCommand.Message as SocketMessage; dcInfo += $"Last Message: \n" + $"Time: `{msg.CreatedAt}`\n" + $"Author: {msg.Author} (`{msg.Author.Id}`)\n" + $"Guild: {msg.GetGuild().Name} (`{msg.GetGuild().Id}`)\n" + $"Channel: #{msg.Channel.Name} (`{msg.Channel.Id}`)\n" + $"Content: `{msg.Content}`\n"; shard.GetApplicationInfoAsync().Result.Owner.SendMessageAsync(dcInfo); Disconnects++; }; } DiscordClient.MessageReceived += MessageEventHandler.MessageRecieved; DiscordClient.MessageDeleted += MessageEventHandler.MessageDeleted; DiscordClient.JoinedGuild += GuildEventHandler.OnJoinedGuild; DiscordClient.LeftGuild += GuildEventHandler.OnLeftGuild; DiscordClient.UserLeft += UserEventHandler.UserLeft; var serviceProvider = ConfigureServices(); var _handler = new CommandHandler(); await _handler.Install(serviceProvider); Updater.Start(); // Block this program until it is closed. await Task.Delay(-1); }