Example #1
0
        /// <summary>
        /// This is the method that will be called when the user sends a message in the bot channel if the interface is waiting on a message.
        /// </summary>
        /// <remarks>
        /// It is guaranteed that the message is from the correct user in the correct channel when this is called.
        /// </remarks>
        /// -C
        /// <param name="message">The message that was sent by the user</param>
        public virtual async Task PerformMessageAction(SocketUserMessage message)
        {
            if (!InputFields.ContainsKey(PageNum) || !InputFields[PageNum].ContainsKey(CursorPos))
            {
                return;                                                                                     // Safeguard
            }
            MessageFieldNode mfn = InputFields[PageNum][CursorPos];
            string           msg = message.Content;

            switch (mfn.Type)
            {
            case FieldDataType.BOOL:
                bool b_input;

                if (!bool.TryParse(msg, out b_input))
                {
                    return;      // If it's invalid, return
                }
                mfn.SetValue(b_input.ToString());
                break;

            case FieldDataType.INT:
                int i_input;

                if (!int.TryParse(msg, out i_input))
                {
                    return;
                }
                mfn.SetValue(i_input.ToString());
                break;

            case FieldDataType.ULONG:
                ulong u_input;

                if (!ulong.TryParse(msg, out u_input))
                {
                    return;
                }
                mfn.SetValue(u_input.ToString());
                break;

            case FieldDataType.DBL:
                double d_input;

                if (!double.TryParse(msg, out d_input))
                {
                    return;      // If it's invalid, return
                }
                mfn.SetValue(d_input.ToString());
                break;

            default:
                mfn.SetValue(msg);
                break;
            }

            await Message.ModifyAsync(_msg => _msg.Embed = GetEmbed()); // update the embed
        }
Example #2
0
        /// <summary>
        /// Adds the embed fields to the embed, with the cursor.
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// -C
        /// <param name="builder">The embed builder</param>
        protected virtual void AddEmbedFields(EmbedBuilder builder)
        {
            if (!InputFields.ContainsKey(PageNum))
            {
                return;
            }

            foreach (int index in InputFields[PageNum].Keys)
            {
                // Add them as text
                MessageFieldNode mfn = InputFields[PageNum][index];

                if (index == CursorPos)
                {
                    builder.AddField($"{CustomEmotes.CursorAnimated} {mfn.Name}", mfn.GetValue());
                }
                else
                {
                    builder.AddField(CustomEmotes.CursorBlankSpace + mfn.Name, mfn.GetValue());
                }
            }
        }
Example #3
0
        /// <summary>
        /// Searches the class for all fields marked with <see cref="InputField"/>,
        /// and adds them to the internal dictionary for use in other methods.
        /// </summary>
        /// <remarks>
        /// Important to note:
        /// Be careful with how you arrange these fields.
        /// If a field is in a spot that is already occupied,
        /// It will throw an error.
        /// </remarks>
        /// -C
        protected void RegisterMenuFields()
        {
            bool arrows = false;

            foreach (FieldInfo f in GetType().GetFields())
            {
                if (Attribute.IsDefined(f, typeof(InputField)))
                {
                    // if it's an input field var
                    InputField info = f.GetCustomAttribute(typeof(InputField)) as InputField; // get it's attribute

                    int page = info.Page;
                    int pos  = info.Position;

                    if (!InputFields.ContainsKey(page))
                    {
                        InputFields[page] = new Dictionary <int, MessageFieldNode>();
                    }

                    if (InputFields[page].ContainsKey(pos))
                    {
                        // If there is already an input field here
                        throw new ConflictingFieldException(InputFields[page][pos].Name, info.Name, page, pos); // Throw an error.
                    }

                    // Otherwise, if everything is fine,
                    // Add the field.
                    MessageFieldNode node = new MessageFieldNode(info.Name, info.Page, info.Position, info.Value, info.Type); // create the node
                    node.ClassPtr = this;                                                                                     // give it a pointer to this class, so it can modify the variable it's attached to
                    node.SetValue(info.Value);
                    InputFields[info.Page][info.Position] = node;                                                             // And add the appropriate node to the dict where it belongs
                }
            }

            int FieldCount = 0;

            foreach (int i in InputFields.Keys)
            {
                foreach (int j in InputFields[i].Keys)
                {
                    FieldCount++;
                    if (FieldCount > 1)
                    {
                        arrows = true;
                    }
                }

                FieldCount = 0;
            }

            if (MenuOptions == null)
            {
                MenuOptions = new List <MenuOptionNode>();
            }

            if (arrows)
            {
                // if there is more than one field, add up and down buttons
                if (!MenuOptions.Contains(ReactionHandler.UP))
                {
                    AddMenuOptions(ReactionHandler.UP);
                }

                if (!MenuOptions.Contains(ReactionHandler.DOWN))
                {
                    AddMenuOptions(ReactionHandler.DOWN);
                }
            }

            PageCount = InputFields.Values.Count; // set the number of pages
        }