Exemple #1
0
        /// <summary>
        /// Handles the given socket component interaction
        /// </summary>
        /// <param name="component">The interaction that occurred</param>
        /// <returns>A task representing the completion of the interaction</returns>
        public async Task HandleComponentAsync(SocketMessageComponent component)
        {
            if (component.User.IsBot ||
                component.User.Id == _client.CurrentUser.Id)
            {
                await component.DeferAsync(ephemeral : true);

                return;
            }

            var id = component.Data.CustomId;

            if (!_handlers.GetHandler(id, out var method, out var type))
            {
                _logger.LogWarning($"Cannot find loaded button for `{id}`");
                await component.DeferAsync(ephemeral : true);

                return;
            }

            if (method == null || type == null)
            {
                _logger.LogWarning($"Method or type is null for `{id}`");
                await component.DeferAsync(ephemeral : true);

                return;
            }

            var service = _provider.GetService(type);

            if (service == null)
            {
                _logger.LogWarning($"Cannot find service for type `{type.Name}`");
                await component.DeferAsync(ephemeral : true);

                return;
            }

            ((ComponentHandler)service).SetContext(component, _client);

            var res = method.Invoke(service, Array.Empty <object>());

            if (res == null)
            {
                return;
            }

            if (method.ReturnType == typeof(Task) ||
                method.ReturnType == typeof(Task <>))
            {
                await(Task) res;
            }
        }