コード例 #1
0
        /// <summary>
        /// Gets all of the <see cref="SelectMenuOptionAttribute"/> attributes from the given method
        /// </summary>
        /// <param name="method">The method to get the attributes from</param>
        /// <returns>A collection of all of the select menu options</returns>
        public SelectMenuOptionBuilder[] MenuOptionsFromAttributes(MethodInfo method)
        {
            var atrs = method.GetCustomAttributes <SelectMenuOptionAttribute>()?.ToArray();

            if (atrs == null || atrs.Length == 0)
            {
                return(Array.Empty <SelectMenuOptionBuilder>());
            }

            return(atrs.Select(t =>
            {
                var builder = new SelectMenuOptionBuilder()
                              .WithLabel(t.Label)
                              .WithValue(t.Value);

                if (!string.IsNullOrEmpty(t.Description))
                {
                    builder.WithValue(t.Description);
                }

                if (!string.IsNullOrEmpty(t.Emote))
                {
                    builder.WithEmote(t.Emote.GetEmote());
                }

                if (t is DefaultSelectMenuOptionAttribute)
                {
                    builder.WithDefault(true);
                }

                return builder;
            }).ToArray());
        }
コード例 #2
0
        public async Task Unsubscribe()
        {
            //Ensure subscriptions are enabled:
            if (!_subscriptions.ModuleEnabled)
            {
                await RespondAsync("Subscriptions module is currently disabled.", ephemeral : true);

                return;
            }

            //Buy more time to process:
            await DeferAsync(false);

            // Get Accounts:
            var subs = await _subscriptions.GuildSubscriptionsAsync(Context.Guild.Id);

            // Create Dropdown with channels:
            var menuBuilder = new SelectMenuBuilder()
                              .WithCustomId("unsubscribe")
                              .WithPlaceholder("Select accounts to remove.")
                              .WithMinValues(0);

            // Get IG account:
            InstagramProcessor instagram = new InstagramProcessor(InstagramProcessor.AccountFinder.GetIGAccount());

            // Add users to dropdown:
            foreach (FollowedIGUser user in subs)
            {
                foreach (RespondChannel chan in user.SubscribedChannels)
                {
                    // Get username:
                    string username = await instagram.GetIGUsername(user.InstagramID);

                    if (username == null)
                    {
                        username = "******";
                    }

                    string channelName = Context.Guild.GetChannel(ulong.Parse(chan.ChannelID))?.Name;
                    // Channel null check:
                    if (channelName is null)
                    {
                        channelName = "Unknown Channel";
                    }

                    // Add account option to menu:
                    SelectMenuOptionBuilder optBuilder = new SelectMenuOptionBuilder()
                                                         .WithLabel(username)
                                                         .WithValue(user.InstagramID + "-" + chan.ChannelID)
                                                         .WithDescription(username + " in channel " + channelName);
                    menuBuilder.AddOption(optBuilder);
                }
            }

            // Check for subs:
            if (subs.Length < 1)
            {
                await FollowupAsync("No accounts subscribed.");

                return;
            }

            // Make embed:
            var embed = new EmbedBuilder();

            embed.Title       = "Unsubscribe";
            embed.Description = "Select accounts that you would like to unsubscribe from in the dropdown below.";
            embed.WithColor(new Color(131, 58, 180));

            // Set max count:
            menuBuilder.WithMaxValues(menuBuilder.Options.Count);
            // Component Builder:
            var builder = new ComponentBuilder()
                          .WithSelectMenu(menuBuilder)
                          .WithButton("Delete Message", $"delete-message-{Context.User.Id}", style: ButtonStyle.Danger);

            // Send message
            await FollowupAsync(embed : embed.Build(), components : builder.Build());
        }