Advertisement
parabola949

Untitled

Apr 10th, 2020
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.99 KB | None | 0 0
  1.         public static InlineKeyboardMarkup CreateMarkupFromMenu(Menu menu)
  2.         {
  3.             if (menu == null) return null;
  4.             var col = menu.Columns - 1;
  5.             var final = new List<InlineKeyboardButton[]>();
  6.             for (var i = 0; i < menu.Buttons.Count; i++)
  7.             {
  8.                 var row = new List<InlineKeyboardButton>();
  9.                 do
  10.                 {
  11.                     var cur = menu.Buttons[i];
  12.                     row.Add(new InlineKeyboardButton
  13.                     {
  14.                         Text = cur.Text,
  15.                         CallbackData = $"{cur.Trigger}|{cur.ExtraData}",
  16.                         Url = cur.Url
  17.                     });
  18.                     i++;
  19.                     if (i == menu.Buttons.Count) break;
  20.                 } while (i % (col + 1) != 0);
  21.                 i--;
  22.                 final.Add(row.ToArray());
  23.                 if (i == menu.Buttons.Count) break;
  24.             }
  25.             return new InlineKeyboardMarkup(final.ToArray());
  26.         }
  27.  
  28.     public class InlineButton
  29.     {
  30.         /// <summary>
  31.         /// The button text
  32.         /// </summary>
  33.         public string Text { get; set; }
  34.         /// <summary>
  35.         /// What trigger to associate this button with.  Make sure you create a CallbackCommand with the trigger set (Optional)
  36.         /// </summary>
  37.         public string Trigger { get; set; }
  38.         /// <summary>
  39.         /// Any extra data you want to include, not visible to the user (Optional)
  40.         /// </summary>
  41.         public string ExtraData { get; set; }
  42.         /// <summary>
  43.         /// Have this button link to a chat or website. (Optional)
  44.         /// </summary>
  45.         public string Url { get; set; }
  46.  
  47.         public InlineButton(string text, string trigger = "", string extraData = "", string url = "")
  48.         {
  49.             Url = url;
  50.             Text = text;
  51.             Trigger = trigger;
  52.             ExtraData = extraData;
  53.         }
  54.     }
  55.  
  56.     public class Menu
  57.     {
  58.         /// <summary>
  59.         /// The buttons you want in your menu
  60.         /// </summary>
  61.         public List<InlineButton> Buttons { get; set; }
  62.         /// <summary>
  63.         /// How many columns.  Defaults to 1.
  64.         /// </summary>
  65.         public int Columns { get; set; }
  66.  
  67.         public Menu(int col = 1, List<InlineButton> buttons = null)
  68.         {
  69.             Buttons = buttons ?? new List<InlineButton>();
  70.             Columns = Math.Max(col, 1);
  71.         }
  72.     }
  73.  
  74.  
  75. //Sample usage
  76.         private static Menu CreateMenu(Group g, Instance db)
  77.         {
  78.             var menu = new Menu(2, new List<InlineButton>());
  79.             menu.Buttons.Add(new InlineButton("Auto Kick Settings", "autokick", g.ID.ToString()));
  80.             var nsfw = g.GetSetting<bool>("NSFW", db, false);
  81.             menu.Buttons.Add(new InlineButton($"NSFW : {(nsfw ? "" : "🚫")}", "ts", $"{g.ID}|NSFW"));
  82.  
  83.             menu.Buttons.Add(new InlineButton("Done", "done"));
  84.             return menu;
  85.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement