using App.Metrics; using Autofac; using Myriad.Cache; using Myriad.Gateway; using Myriad.Rest; using NodaTime; using PluralKit.Core; using Sentry; using Serilog; using IClock = NodaTime.IClock; namespace PluralKit.Bot; public class BotModule: Module { protected override void Load(ContainerBuilder builder) { // Clients builder.Register(c => { var botConfig = c.Resolve(); return new GatewaySettings { Token = botConfig.Token, MaxShardConcurrency = botConfig.MaxShardConcurrency, GatewayQueueUrl = botConfig.GatewayQueueUrl, Intents = GatewayIntent.Guilds | GatewayIntent.DirectMessages | GatewayIntent.DirectMessageReactions | GatewayIntent.GuildEmojis | GatewayIntent.GuildMessages | GatewayIntent.GuildWebhooks | GatewayIntent.GuildMessageReactions }; }).AsSelf().SingleInstance(); builder.RegisterType().AsSelf().SingleInstance(); builder.Register(c => { return new MemoryDiscordCache(); }).AsSelf().As().SingleInstance(); builder.Register(c => { var client = new DiscordApiClient( c.Resolve().Token, c.Resolve(), c.Resolve().DiscordBaseUrl ); var metrics = c.Resolve(); client.OnResponseEvent += (_, ev) => { var (endpoint, statusCode, ticks) = ev; var timer = metrics.Provider.Timer.Instance(BotMetrics.DiscordApiRequests, new MetricTags( new[] { "endpoint", "status_code" }, new[] { endpoint, statusCode.ToString() } )); timer.Record(ticks / 10, TimeUnit.Microseconds); }; return client; }).AsSelf().SingleInstance(); // Commands builder.RegisterType().AsSelf(); builder.RegisterType().AsSelf(); builder.RegisterType().AsSelf(); builder.RegisterType().AsSelf(); builder.RegisterType().AsSelf(); builder.RegisterType().AsSelf(); builder.RegisterType().AsSelf(); builder.RegisterType().AsSelf(); builder.RegisterType().AsSelf(); builder.RegisterType().AsSelf(); builder.RegisterType().AsSelf(); builder.RegisterType().AsSelf(); builder.RegisterType().AsSelf(); builder.RegisterType().AsSelf(); builder.RegisterType().AsSelf(); builder.RegisterType().AsSelf(); builder.RegisterType().AsSelf(); builder.RegisterType().AsSelf(); builder.RegisterType().AsSelf(); builder.RegisterType().AsSelf(); builder.RegisterType().AsSelf(); builder.RegisterType().AsSelf(); builder.RegisterType().AsSelf(); builder.RegisterType().AsSelf(); // Bot core builder.RegisterType().AsSelf().SingleInstance(); builder.RegisterType().As>(); builder.RegisterType().As>() .As>(); builder.RegisterType().As>(); builder.RegisterType().As>(); builder.RegisterType().As>(); // Event handler queue builder.RegisterType>().AsSelf().SingleInstance(); builder.RegisterType>().AsSelf().SingleInstance(); // Bot services builder.RegisterType().AsSelf().SingleInstance(); builder.RegisterType().AsSelf().SingleInstance(); builder.RegisterType().AsSelf().SingleInstance(); builder.RegisterType().AsSelf().SingleInstance(); builder.RegisterType().AsSelf().SingleInstance(); builder.RegisterType().AsSelf().SingleInstance(); builder.RegisterType().AsSelf().SingleInstance(); builder.RegisterType().AsSelf().SingleInstance(); builder.RegisterType().AsSelf().SingleInstance(); builder.RegisterType().AsSelf().SingleInstance(); builder.RegisterType().AsSelf().SingleInstance(); builder.RegisterType().AsSelf().SingleInstance(); builder.RegisterType().AsSelf().SingleInstance(); builder.RegisterType().AsSelf().SingleInstance(); // Sentry stuff builder.Register(_ => new Scope(null)).AsSelf().InstancePerLifetimeScope(); builder.RegisterType() .As>() .As>() .As>() .As>() .As>() .SingleInstance(); // Proxy stuff builder.RegisterType().AsSelf().SingleInstance(); builder.RegisterType().AsSelf().SingleInstance(); // Utils builder.Register(c => new HttpClient { Timeout = TimeSpan.FromSeconds(5), DefaultRequestHeaders = { { "User-Agent", DiscordApiClient.UserAgent } } }).AsSelf().SingleInstance(); builder.RegisterInstance(SystemClock.Instance).As(); builder.RegisterType().AsSelf().SingleInstance(); } }