Exemple #1
0
        public void ConfigureServices(IServiceCollection services)
        {
            var connectionString = Configuration.GetConnectionString("Default");
            var basePath         = Configuration.GetValue <string>("FilesBasePath");

            services
            .AddFileSystem(builder => builder.Use(basePath))
            .AddDatabase(connectionString)
            .AddWebAuthentication()
            .AddHandlers()
            .AddLoggers()
            .AddMemoryCache()
            .AddCors()
            .AddMessageCache()
            .AddHttpClient()
            .AddScoped <FileExtensionContentTypeProvider>()
            .AddControllersWithViews();

            var pages = services.AddRazorPages();

#if DEBUG
            pages.AddRazorRuntimeCompilation();
#endif

            const GatewayIntents intents = GatewayIntents.Guilds | GatewayIntents.GuildMembers | GatewayIntents.GuildBans | GatewayIntents.GuildEmojis | GatewayIntents.GuildIntegrations | GatewayIntents.GuildInvites |
                                           GatewayIntents.GuildVoiceStates | GatewayIntents.GuildPresences | GatewayIntents.GuildMessages | GatewayIntents.GuildMessageReactions | GatewayIntents.GuildMessageTyping | GatewayIntents.DirectMessages |
                                           GatewayIntents.DirectMessageReactions | GatewayIntents.DirectMessageTyping;
            var config = new DiscordSocketConfig()
            {
                LogLevel         = LogSeverity.Verbose,
                MessageCacheSize = 50000,
                GatewayIntents   = intents
            };

            var commandsConfig = new CommandServiceConfig()
            {
                LogLevel              = LogSeverity.Verbose,
                DefaultRunMode        = RunMode.Async,
                CaseSensitiveCommands = true
            };

            services
            .AddSingleton(new CommandService(commandsConfig))
            .AddSingleton(new DiscordSocketClient(config))
            .AddSingleton <InitService>()
            .AddSingleton(new BotState())
            .AddBotFeatures()
            .AddEmoteChain()
            .AddStatistics()
            .AddPaginationServices()
            .AddDuckServices()
            .AddHelpServices()
            .AddBackgroundTasks()
            .AddHostedService <GrillBotService>();

            services
            .Configure <WebAdminConfiguration>(Configuration.GetSection("WebAdmin"));
        }
        /// <summary>
        /// Starts a connection to discord with the given apiKey and intents
        /// </summary>
        /// <param name="apiKey">API key for the connecting bot</param>
        /// <param name="intents">Intents the bot needs in order to function</param>
        public void Connect(string apiKey, GatewayIntents intents)
        {
            DiscordSettings settings = new DiscordSettings
            {
                ApiToken = apiKey,
                LogLevel = DiscordLogLevel.Info,
                Intents  = intents
            };

            Connect(settings);
        }
        /// <summary>
        /// Add a client to this bot client
        /// </summary>
        /// <param name="client">Client to add to the bot</param>
        public void AddClient(DiscordClient client)
        {
            if (Settings.ApiToken != client.Settings.ApiToken)
            {
                throw new Exception("Failed to add client to bot client as ApiTokens do not match");
            }

            _clients.RemoveAll(c => c == client);
            _clients.Add(client);

            Logger.Debug($"{nameof(BotClient)}.{nameof(AddClient)} Add client for plugin {client.Owner.Title}");

            if (_clients.Count == 1)
            {
                Logger.Debug($"{nameof(BotClient)}.{nameof(AddClient)} Clients.Count == 1 connecting bot");
                ConnectWebSocket();
                return;
            }

            if (client.Settings.LogLevel < Settings.LogLevel)
            {
                UpdateLogLevel(client.Settings.LogLevel);
            }

            GatewayIntents intents = Settings.Intents | client.Settings.Intents;

            //Our intents have changed. Disconnect websocket and reconnect with new intents.
            if (intents != Settings.Intents)
            {
                Logger.Info("New intents have been requested for the bot. Reconnecting with updated intents.");
                Settings.Intents = intents;
                DisconnectWebsocket(true);
            }

            if (ReadyData != null)
            {
                ReadyData.Guilds = Servers.Copy();
                client.CallHook(DiscordExtHooks.OnDiscordGatewayReady, ReadyData);

                foreach (DiscordGuild guild in Servers.Values)
                {
                    if (guild.IsAvailable)
                    {
                        client.CallHook(DiscordExtHooks.OnDiscordGuildCreated, guild);
                    }

                    if (guild.HasLoadedAllMembers)
                    {
                        client.CallHook(DiscordExtHooks.OnDiscordGuildMembersLoaded, guild);
                    }
                }
            }
        }
Exemple #4
0
        public void ConfigureServices(IServiceCollection services)
        {
            var connectionString = Configuration.GetValue <string>("DB_CONN");

            services
            .AddDatabase(connectionString)
            .AddWebAuthentication()
            .AddHandlers()
            .AddLoggers()
            .AddMemoryCache()
            .AddCors()
            .AddMessageCache()
            .AddHttpClient();

            services.AddSwaggerGen(setup =>
            {
                var apiInfo = new OpenApiInfo()
                {
                    Contact = new OpenApiContact()
                    {
                        Name = "GrillBot",
                        Url  = new Uri(ThisAssembly.Git.RepositoryUrl)
                    },
                    License = new OpenApiLicense()
                    {
                        Name = "MIT"
                    },
                    Title   = "GrillBot API",
                    Version = "v1"
                };

                setup.SwaggerDoc("v1", apiInfo);

                setup.AddSecurityDefinition("GrillBot", new OpenApiSecurityScheme()
                {
                    Description = "GrillBot authorization token. BotAdmin user can generate this tokens for user (or bot) accounts.",
                    In          = ParameterLocation.Header,
                    Name        = "Authorization",
                    Scheme      = "GrillBot",
                    Type        = SecuritySchemeType.ApiKey
                });

                setup.AddSecurityRequirement(new OpenApiSecurityRequirement()
                {
                    {
                        new OpenApiSecurityScheme()
                        {
                            Reference = new OpenApiReference()
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id   = "GrillBot"
                            }
                        }, new List <string>()
                    }
                });
            });

            services
            .AddControllersWithViews();

            var pages = services.AddRazorPages();

#if DEBUG
            pages.AddRazorRuntimeCompilation();
#endif

            const GatewayIntents intents = GatewayIntents.Guilds | GatewayIntents.GuildMembers | GatewayIntents.GuildBans | GatewayIntents.GuildEmojis | GatewayIntents.GuildIntegrations | GatewayIntents.GuildInvites |
                                           GatewayIntents.GuildVoiceStates | GatewayIntents.GuildPresences | GatewayIntents.GuildMessages | GatewayIntents.GuildMessageReactions | GatewayIntents.GuildMessageTyping | GatewayIntents.DirectMessages |
                                           GatewayIntents.DirectMessageReactions | GatewayIntents.DirectMessageTyping;
            var config = new DiscordSocketConfig()
            {
                LogLevel         = LogSeverity.Verbose,
                MessageCacheSize = 50000,
                GatewayIntents   = intents
            };

            var commandsConfig = new CommandServiceConfig()
            {
                LogLevel              = LogSeverity.Verbose,
                DefaultRunMode        = RunMode.Async,
                CaseSensitiveCommands = true
            };

            services
            .AddSingleton(new CommandService(commandsConfig))
            .AddSingleton(new DiscordSocketClient(config))
            .AddSingleton <InitService>()
            .AddSingleton(new BotState());

            services
            .AddBotFeatures()
            .AddMath()
            .AddEmoteChain()
            .AddStatistics()
            .AddPaginationServices()
            .AddDuckServices()
            .AddHelpServices()
            .AddBackgroundTasks();

            services
            .AddHostedService <GrillBotService>();
        }
Exemple #5
0
 public async Task SendIdentifyAsync(int largeThreshold = 100, int shardID = 0, int totalShards = 1, GatewayIntents gatewayIntents = GatewayIntents.AllUnprivileged, (UserStatus, bool, long?, GameModel)?presence = null, RequestOptions options = null)
Exemple #6
0
 public Identity(Token token, GatewayIntents intents)
 {
     Token   = token;
     Intents = intents;
 }
Exemple #7
0
 public Identity(Token token, GatewayIntents intents, Option <Shard> shard)
 {
     Token   = token;
     Intents = intents;
     Shard   = shard;
 }