Example #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;
            }
        }
        private async Task MusicPlayerButtonExecutedAsync(SocketMessageComponent component)
        {
            var userVoiceChannel = (component.User as IGuildUser).VoiceChannel;

            if (userVoiceChannel is null)
            {
                return;
            }

            var botUser = await userVoiceChannel.GetUserAsync(_client.CurrentUser.Id);

            if (botUser is null)
            {
                return;
            }

            switch (component.Data.CustomId)
            {
            case "MusicPlayerShuffle":
                _musicPlayer.Shuffle();
                await component.DeferAsync();

                break;

            case "MusicPlayerPrevious":
                _musicPlayer.Previous();
                await component.DeferAsync();

                break;

            case "MusicPlayerContinue":
                _musicPlayer.Continue();
                await component.DeferAsync();

                break;

            case "MusicPlayerPause":
                _musicPlayer.Pause();
                await component.DeferAsync();

                break;

            case "MusicPlayerStop":
                _musicPlayer.Stop();
                await component.DeferAsync();

                break;

            case "MusicPlayerNext":
                _musicPlayer.Next();
                await component.DeferAsync();

                break;

            default: break;
            }
        }
        private async Task ActivityButtonExecutedAsync(SocketMessageComponent component)
        {
            var user = await _client.Rest.GetGuildUserAsync((component.Channel as IGuildChannel).GuildId, component.User.Id);

            if (!user.RoleIds.Any(id => _destinyRoleIDs.Any(x => x == id)))
            {
                return;
            }

            switch (component.Data.CustomId)
            {
            case "ActivitierSubscribe":
            {
                await component.DeferAsync();

                await _activityManager.UserSubscribeOrUnsubscribeAsync(component.Message.Id, component.User.Id);
            }
            break;

            default: break;
            }
        }
Example #4
0
        private async Task ActivitySelectMenuExecutedAsync(SocketMessageComponent component)
        {
            switch (component.Data.CustomId)
            {
            case "QuickActivitySelector":
            {
                var builder = new EmbedBuilder()
                              .WithColor(new Color(0xFFFFFF))
                              .WithDescription("Оберіть зручний час");

                var menuBuilder = new SelectMenuBuilder()
                                  .WithPlaceholder("Оберіть час")
                                  .WithCustomId(string.Join(',', component.Data.Values))
                                  .WithMinValues(1).WithMaxValues(1);

                var startDate = DateTime.Now;
                var endDate   = startDate.AddHours(12);
                var tmpDate   = startDate.AddMinutes(30);

                while (tmpDate < endDate)
                {
                    menuBuilder = menuBuilder.AddOption(tmpDate.ToString("dd.MM HH:mm"), tmpDate.ToString("dd.MM_HH:mm"));
                    tmpDate     = tmpDate.AddMinutes(30);
                }

                var componentBuilder = new ComponentBuilder()
                                       .WithSelectMenu(menuBuilder);

                await component.RespondAsync(embed : builder.Build(), components : componentBuilder.Build(), ephemeral : true);
            }
            break;

            case string c
                when c.StartsWith("QuickActivity_"):
            {
                var raid = new ActivityContainer()
                {
                    ChannelID    = component.Channel.Id,
                    ActivityType = Enum.Parse <BungieSharper.Entities.Destiny.HistoricalStats.Definitions.DestinyActivityModeType>(c.Split('_')[1]),
                    PlannedDate  = DateTime.ParseExact(string.Join(',', component.Data.Values), "dd.MM_HH:mm", CultureInfo.CurrentCulture),
                    ActivityName = null,
                    Description  = null,
                    Users        = new ulong[] { component.User.Id }
                };

                await component.DeferAsync();

                await InitActivityAsync(raid);
            }
            break;

            case string c
                when c.StartsWith("QuickRaid_"):
            {
                var raid = new ActivityContainer()
                {
                    ChannelID    = component.Channel.Id,
                    ActivityType = BungieSharper.Entities.Destiny.HistoricalStats.Definitions.DestinyActivityModeType.Raid,
                    PlannedDate  = DateTime.ParseExact(string.Join(',', component.Data.Values), "dd.MM_HH:mm", CultureInfo.CurrentCulture),
                    ActivityName = c.Split('_')[1],
                    Description  = null,
                    Users        = new ulong[] { component.User.Id }
                };

                await component.DeferAsync();

                await InitActivityAsync(raid);
            }
            break;

            default: break;
            }
        }