コード例 #1
0
        public static Task RunMenuAsync(this DiscordClientBase client, Snowflake channelId, MenuBase menu, TimeSpan timeout = default, CancellationToken cancellationToken = default)
        {
            var extension = client.GetInteractivity();

            return(extension.RunMenuAsync(channelId, menu, timeout, cancellationToken));
        }
コード例 #2
0
        static ISynchronizedDictionary <IEmoji, Button> GetButtons(MenuBase menu)
        {
            var methods = _typeCache.GetOrAdd(menu.GetType(), static x =>
            {
                var methods = x.GetMethods(BindingFlags.Public | BindingFlags.Instance);
                IDictionary <IEmoji, MethodInfo> buttons = null;
                for (var i = 0; i < methods.Length; i++)
                {
                    var method          = methods[i];
                    var buttonAttribute = method.GetCustomAttribute <ButtonAttribute>();
                    if (buttonAttribute == null)
                    {
                        continue;
                    }

                    if (method.ContainsGenericParameters)
                    {
                        throw new InvalidOperationException("A button callback must not contain generic parameters.");
                    }

                    if (method.ReturnType != typeof(ValueTask))
                    {
                        throw new InvalidOperationException("A button callback must return a non-generic ValueTask.");
                    }

                    var parameters = method.GetParameters();
                    if (parameters.Length != 1 || parameters[0].ParameterType != typeof(ButtonEventArgs))
                    {
                        throw new InvalidOperationException("A button callback must contain a single ButtonEventArgs parameter.");
                    }

                    if (buttons == null)
                    {
                        buttons = new Dictionary <IEmoji, MethodInfo>();
                    }

                    buttons.Add(buttonAttribute.Emoji, method);
                }

                KeyValuePair <IEmoji, MethodInfo>[] array;
                if (buttons != null)
                {
                    array = new KeyValuePair <IEmoji, MethodInfo> [buttons.Count];
                    buttons.CopyTo(array, 0);
                }
                else
                {
                    array = Array.Empty <KeyValuePair <IEmoji, MethodInfo> >();
                }

                return(array);
            });

            var buttons = new SynchronizedDictionary <IEmoji, Button>(methods.Length);

            for (var i = 0; i < methods.Length; i++)
            {
                var(emoji, method) = methods[i];
                var button = ButtonFactory(emoji, method, i, menu);
                buttons.Add(button.Emoji, button);
            }

            return(buttons);
        }