internal CallbackQueryEventArgs(Update update) { CallbackQuery = update.CallbackQuery; }
internal CallbackQueryEventArgs(CallbackQuery callbackQuery) { CallbackQuery = callbackQuery; }
public void SendReply(CallbackQuery query) { var info = JsonConvert.SerializeObject(new CallbackInfo {Query = query}); this.Broadcast(info); }
internal static void HandleCallback(CallbackQuery query) { using (var DB = new WWContext()) { try { string[] args = query.Data.Split('|'); InlineKeyboardMarkup menu; Group grp; List<InlineKeyboardButton> buttons = new List<InlineKeyboardButton>(); long groupid = 0; if (args[0] == "vote") { var node = Bot.Nodes.FirstOrDefault(x => x.ClientId.ToString() == args[1]); node?.SendReply(query); return; } groupid = long.Parse(args[1]); grp = DB.Groups.FirstOrDefault(x => x.GroupId == groupid); if (grp == null && args[0] != "getlang" && args[0] != "validate") return; var command = args[0]; var choice = ""; if (args.Length > 2) choice = args[2]; if (choice == "cancel") { Bot.Api.EditMessageText(query.Message.Chat.Id, query.Message.MessageId, $"What would you like to do?", replyMarkup: GetConfigMenu(groupid)); return; } if (!nonCommandsList.Contains(command.ToLower())) if (!UpdateHelper.IsGroupAdmin(query.From.Id, groupid)) { Bot.Api.EditMessageText(query.Message.Chat.Id, query.Message.MessageId, "You do not appear to be an admin"); return; } switch (command) { case "validate": //choice = args[1]; if (choice == "All") { Helpers.LanguageHelper.ValidateFiles(query.Message.Chat.Id, query.Message.MessageId); return; } //var menu = new ReplyKeyboardHide { HideKeyboard = true, Selective = true }; //Bot.SendTextMessage(id, "", replyToMessageId: update.Message.MessageId, replyMarkup: menu); var langOptions = Directory.GetFiles(Bot.LanguageDirectory) .Select( x => new { Name = XDocument.Load(x) .Descendants("language") .First() .Attribute("name") .Value, FilePath = x }); var option = langOptions.First(x => x.Name == choice); LanguageHelper.ValidateLanguageFile(query.Message.Chat.Id, option.FilePath, query.Message.MessageId); return; case "getlang": Bot.Api.EditMessageText(query.Message.Chat.Id, query.Message.MessageId, "One moment..."); if (choice == "All") LanguageHelper.SendAllFiles(query.Message.Chat.Id); else LanguageHelper.SendFile(query.Message.Chat.Id, choice); break; case "upload": Console.WriteLine(choice); if (choice == "current") { Bot.Api.EditMessageText(query.Message.Chat.Id, query.Message.MessageId, "No action taken."); return; } Helpers.LanguageHelper.UseNewLanguageFile(choice, query.Message.Chat.Id, query.Message.MessageId); return; case "vote": //send it back to the game; var node = Bot.Nodes.FirstOrDefault(x => x.ClientId.ToString() == args[1]); node?.SendReply(query); break; case "lang": //load up each file and get the names var langs = Directory.GetFiles(Bot.LanguageDirectory) .Select( x => new { Name = XDocument.Load(x) .Descendants("language") .First() .Attribute("name") .Value, Base = XDocument.Load(x) .Descendants("language") .First() .Attribute("base") .Value, Variant = XDocument.Load(x) .Descendants("language") .First() .Attribute("variant") .Value, FileName = Path.GetFileNameWithoutExtension(x) }); buttons.Clear(); buttons.AddRange(langs.Select(x => x.Base).Distinct().OrderBy(x => x).Select(x => new InlineKeyboardButton(x, $"setlang|{groupid}|{x}|null|base"))); var baseMenu = new List<InlineKeyboardButton[]>(); for (var i = 0; i < buttons.Count; i++) { if (buttons.Count - 1 == i) { baseMenu.Add(new[] { buttons[i] }); } else baseMenu.Add(new[] { buttons[i], buttons[i + 1] }); i++; } menu = new InlineKeyboardMarkup(baseMenu.ToArray()); var curLang = langs.First(x => x.FileName == grp.Language); Bot.Api.EditMessageText(query.Message.Chat.Id, query.Message.MessageId, $"What Language?\nCurrent: {curLang.Base}", replyMarkup: menu); break; case "setlang": //first, is this the base or variant? var isBase = args[4] == "base"; //ok, they picked a language, let's set it. var validlangs = Directory.GetFiles(Bot.LanguageDirectory) .Select( x => new { Name = XDocument.Load(x) .Descendants("language") .First() .Attribute("name") .Value, Base = XDocument.Load(x) .Descendants("language") .First() .Attribute("base") .Value, Variant = XDocument.Load(x) .Descendants("language") .First() .Attribute("variant") .Value, FileName = Path.GetFileNameWithoutExtension(x) }); //ok, if base we need to check for variants.... var lang = validlangs.First(x => x.Base == choice); if (isBase) { var variants = validlangs.Where(x => x.Base == choice); if (variants.Count() > 1) { buttons.Clear(); buttons.AddRange(variants.Select(x => new InlineKeyboardButton(x.Variant, $"setlang|{groupid}|{x.Base}|{x.Variant}|v"))); var twoMenu = new List<InlineKeyboardButton[]>(); for (var i = 0; i < buttons.Count; i++) { if (buttons.Count - 1 == i) { twoMenu.Add(new[] { buttons[i] }); } else twoMenu.Add(new[] { buttons[i], buttons[i + 1] }); i++; } menu = new InlineKeyboardMarkup(twoMenu.ToArray()); var curVariant = validlangs.First(x => x.FileName == grp.Language); Bot.Api.EditMessageText(query.Message.Chat.Id, query.Message.MessageId, $"What Variant?\nCurrent: {curVariant.Variant}", replyMarkup: menu); return; } //only one variant, move along } else { lang = validlangs.First(x => x.Base == choice && x.Variant == args[3]); } if ( Directory.GetFiles(Bot.LanguageDirectory) .Any( x => String.Equals(Path.GetFileNameWithoutExtension(x), lang.FileName, StringComparison.InvariantCultureIgnoreCase))) { //now get the group grp.Language = lang.FileName; //check for any games running var ig = GetGroupNodeAndGame(groupid); ig?.LoadLanguage(lang.FileName); menu = GetConfigMenu(groupid); Bot.Api.AnswerCallbackQuery(query.Id, $"Language set to {lang.Base}{(String.IsNullOrWhiteSpace(lang.Variant) ? "" : ": " + lang.Variant)}"); Bot.Api.EditMessageText(query.Message.Chat.Id, query.Message.MessageId, $"What would you like to do?", replyMarkup: menu); } DB.SaveChanges(); break; case "online": buttons.Add(new InlineKeyboardButton("Yes", $"setonline|{groupid}|show")); buttons.Add(new InlineKeyboardButton("No", $"setonline|{groupid}|hide")); buttons.Add(new InlineKeyboardButton("Cancel", $"setonline|{groupid}|cancel")); menu = new InlineKeyboardMarkup(buttons.Select(x => new[] { x }).ToArray()); Bot.Api.EditMessageText(query.Message.Chat.Id, query.Message.MessageId, $"Do you want your group to be notified when the bot is online?\nCurrent: {grp.DisableNotification != false}", replyMarkup: menu); break; case "setonline": grp.DisableNotification = (choice == "hide"); Bot.Api.AnswerCallbackQuery(query.Id, $"Notification will {(grp.DisableNotification == true ? "not " : "")}be shown on startup"); Bot.Api.EditMessageText(query.Message.Chat.Id, query.Message.MessageId, $"What would you like to do?", replyMarkup: GetConfigMenu(groupid)); DB.SaveChanges(); break; case "flee": buttons.Add(new InlineKeyboardButton("Yes", $"setflee|{groupid}|enable")); buttons.Add(new InlineKeyboardButton("No", $"setflee|{groupid}|disable")); buttons.Add(new InlineKeyboardButton("Cancel", $"setflee|{groupid}|cancel")); menu = new InlineKeyboardMarkup(buttons.Select(x => new[] { x }).ToArray()); Bot.Api.EditMessageText(query.Message.Chat.Id, query.Message.MessageId, $"Do you want to allow fleeing once the game has started?\nNote: players can still flee during join phase\nCurrent: Players can {(grp.DisableFlee == false ? "" : "not ")}flee", replyMarkup: menu); break; case "setflee": grp.DisableFlee = (choice == "disable"); Bot.Api.AnswerCallbackQuery(query.Id, $"Players will {(grp.DisableFlee == true ? "not " : "")}be allowed to flee after game start"); Bot.Api.EditMessageText(query.Message.Chat.Id, query.Message.MessageId, $"What would you like to do?", replyMarkup: GetConfigMenu(groupid)); DB.SaveChanges(); break; case "maxplayer": buttons.Add(new InlineKeyboardButton("10", $"setmaxplayer|{groupid}|10")); buttons.Add(new InlineKeyboardButton("15", $"setmaxplayer|{groupid}|15")); buttons.Add(new InlineKeyboardButton("20", $"setmaxplayer|{groupid}|20")); buttons.Add(new InlineKeyboardButton("25", $"setmaxplayer|{groupid}|25")); buttons.Add(new InlineKeyboardButton("30", $"setmaxplayer|{groupid}|30")); buttons.Add(new InlineKeyboardButton("35", $"setmaxplayer|{groupid}|35")); buttons.Add(new InlineKeyboardButton("Cancel", $"setmaxplayer|{groupid}|cancel")); menu = new InlineKeyboardMarkup(buttons.Select(x => new[] { x }).ToArray()); Bot.Api.EditMessageText(query.Message.Chat.Id, query.Message.MessageId, $"How many players would like to set as the maximum?\nCurrent: {grp.MaxPlayers ?? Settings.MaxPlayers}", replyMarkup: menu); break; case "setmaxplayer": grp.MaxPlayers = int.Parse(choice); Bot.Api.AnswerCallbackQuery(query.Id, $"Max players set to {choice}"); Bot.Api.EditMessageText(query.Message.Chat.Id, query.Message.MessageId, $"What would you like to do?", replyMarkup: GetConfigMenu(groupid)); DB.SaveChanges(); break; case "roles": buttons.Add(new InlineKeyboardButton("Show", $"setroles|{groupid}|show")); buttons.Add(new InlineKeyboardButton("Hide", $"setroles|{groupid}|hide")); buttons.Add(new InlineKeyboardButton("Cancel", $"setroles|{groupid}|cancel")); menu = new InlineKeyboardMarkup(buttons.Select(x => new[] { x }).ToArray()); Bot.Api.EditMessageText(query.Message.Chat.Id, query.Message.MessageId, $"Show or Hide roles on death?\nCurrent: {(grp.ShowRoles == false ? "Hidden" : "Shown")}", replyMarkup: menu); break; case "setroles": grp.ShowRoles = (choice == "show"); Bot.Api.AnswerCallbackQuery(query.Id, $"Roles will be {(grp.ShowRoles == false ? "hidden" : "shown")} on death."); Bot.Api.EditMessageText(query.Message.Chat.Id, query.Message.MessageId, $"What would you like to do?", replyMarkup: GetConfigMenu(groupid)); DB.SaveChanges(); break; case "mode": buttons.Add(new InlineKeyboardButton("Normal Only", $"setmode|{groupid}|Normal")); buttons.Add(new InlineKeyboardButton("Chaos Only", $"setmode|{groupid}|Chaos")); buttons.Add(new InlineKeyboardButton("Player Choice", $"setmode|{groupid}|Player")); buttons.Add(new InlineKeyboardButton("Cancel", $"setmode|{groupid}|cancel")); menu = new InlineKeyboardMarkup(buttons.Select(x => new[] { x }).ToArray()); Bot.Api.EditMessageText(query.Message.Chat.Id, query.Message.MessageId, $"What game mode will the group be?\nCurrent: {grp.Mode}", replyMarkup: menu); break; case "setmode": grp.Mode = choice; Bot.Api.AnswerCallbackQuery(query.Id, $"Game mode set to {choice}"); Bot.Api.EditMessageText(query.Message.Chat.Id, query.Message.MessageId, $"What would you like to do?", replyMarkup: GetConfigMenu(groupid)); DB.SaveChanges(); break; case "endroles": buttons.Add(new InlineKeyboardButton("Don't show any", $"setendroles|{groupid}|None")); buttons.Add(new InlineKeyboardButton("Show only living players", $"setendroles|{groupid}|Living")); buttons.Add(new InlineKeyboardButton("Show all players", $"setendroles|{groupid}|All")); buttons.Add(new InlineKeyboardButton("Cancel", $"setendroles|{groupid}|cancel")); menu = new InlineKeyboardMarkup(buttons.Select(x => new[] { x }).ToArray()); Bot.Api.EditMessageText(query.Message.Chat.Id, query.Message.MessageId, $"How do you want roles to be shown at the end?\nCurrent: {grp.ShowRolesEnd}", replyMarkup: menu); break; case "setendroles": grp.ShowRolesEnd = choice; Bot.Api.AnswerCallbackQuery(query.Id, $"Roles shown at end set to: {choice}"); Bot.Api.EditMessageText(query.Message.Chat.Id, query.Message.MessageId, $"What would you like to do?", replyMarkup: GetConfigMenu(groupid)); DB.SaveChanges(); break; case "day": buttons.Add(new InlineKeyboardButton("30", $"setday|{groupid}|30")); buttons.Add(new InlineKeyboardButton("60", $"setday|{groupid}|60")); buttons.Add(new InlineKeyboardButton("90", $"setday|{groupid}|90")); buttons.Add(new InlineKeyboardButton("120", $"setday|{groupid}|120")); buttons.Add(new InlineKeyboardButton("Cancel", $"setday|{groupid}|cancel")); menu = new InlineKeyboardMarkup(buttons.Select(x => new[] { x }).ToArray()); Bot.Api.EditMessageText(query.Message.Chat.Id, query.Message.MessageId, $"Choose the base time (in seconds) for day time. This will still be modified based on number of players.\nMinimum time added based on players is 60 seconds. Default setting: {Settings.TimeDay}\nCurrent: {grp.DayTime ?? Settings.TimeDay}", replyMarkup: menu); break; case "setday": grp.DayTime = int.Parse(choice); Bot.Api.AnswerCallbackQuery(query.Id, $"Base day time set to {choice} seconds"); Bot.Api.EditMessageText(query.Message.Chat.Id, query.Message.MessageId, $"What would you like to do?", replyMarkup: GetConfigMenu(groupid)); DB.SaveChanges(); break; case "night": buttons.Add(new InlineKeyboardButton("30", $"setnight|{groupid}|30")); buttons.Add(new InlineKeyboardButton("60", $"setnight|{groupid}|60")); buttons.Add(new InlineKeyboardButton("90", $"setnight|{groupid}|90")); buttons.Add(new InlineKeyboardButton("120", $"setnight|{groupid}|120")); buttons.Add(new InlineKeyboardButton("Cancel", $"setnight|{groupid}|cancel")); menu = new InlineKeyboardMarkup(buttons.Select(x => new[] { x }).ToArray()); Bot.Api.EditMessageText(query.Message.Chat.Id, query.Message.MessageId, $"Choose the time (in seconds) for night time. Default setting: {Settings.TimeNight}\nCurrent:{grp.NightTime ?? Settings.TimeNight}", replyMarkup: menu); break; case "setnight": grp.NightTime = int.Parse(choice); Bot.Api.AnswerCallbackQuery(query.Id, $"Night time set to {choice} seconds"); Bot.Api.EditMessageText(query.Message.Chat.Id, query.Message.MessageId, $"What would you like to do?", replyMarkup: GetConfigMenu(groupid)); DB.SaveChanges(); break; case "lynch": buttons.Add(new InlineKeyboardButton("30", $"setlynch|{groupid}|30")); buttons.Add(new InlineKeyboardButton("60", $"setlynch|{groupid}|60")); buttons.Add(new InlineKeyboardButton("90", $"setlynch|{groupid}|90")); buttons.Add(new InlineKeyboardButton("120", $"setlynch|{groupid}|120")); buttons.Add(new InlineKeyboardButton("Cancel", $"setlynch|{groupid}|cancel")); menu = new InlineKeyboardMarkup(buttons.Select(x => new[] { x }).ToArray()); Bot.Api.EditMessageText(query.Message.Chat.Id, query.Message.MessageId, $"Choose the time (in seconds) for lynch voting. Default setting: {Settings.TimeLynch}\nCurrent:{grp.LynchTime ?? Settings.TimeLynch}", replyMarkup: menu); break; case "setlynch": grp.LynchTime = int.Parse(choice); Bot.Api.AnswerCallbackQuery(query.Id, $"Lynch voting time set to {choice} seconds"); Bot.Api.EditMessageText(query.Message.Chat.Id, query.Message.MessageId, $"What would you like to do?", replyMarkup: GetConfigMenu(groupid)); DB.SaveChanges(); break; case "fool": buttons.Add(new InlineKeyboardButton("Allow", $"setfool|{groupid}|true")); buttons.Add(new InlineKeyboardButton("Disallow", $"setfool|{groupid}|false")); buttons.Add(new InlineKeyboardButton("Cancel", $"setfool|{groupid}|cancel")); menu = new InlineKeyboardMarkup(buttons.Select(x => new[] { x }).ToArray()); Bot.Api.EditMessageText(query.Message.Chat.Id, query.Message.MessageId, $"Allow fool as a role option?\nCurrent: {grp.AllowFool}", replyMarkup: menu); break; case "setfool": grp.AllowFool = (choice == "true"); Bot.Api.AnswerCallbackQuery(query.Id, $"Fool as a role set to: {choice}"); Bot.Api.EditMessageText(query.Message.Chat.Id, query.Message.MessageId, $"What would you like to do?", replyMarkup: GetConfigMenu(groupid)); DB.SaveChanges(); break; case "tanner": buttons.Add(new InlineKeyboardButton("Allow", $"settanner|{groupid}|true")); buttons.Add(new InlineKeyboardButton("Disallow", $"settanner|{groupid}|false")); buttons.Add(new InlineKeyboardButton("Cancel", $"settanner|{groupid}|cancel")); menu = new InlineKeyboardMarkup(buttons.Select(x => new[] { x }).ToArray()); Bot.Api.EditMessageText(query.Message.Chat.Id, query.Message.MessageId, $"Allow tanner as a role option?\nCurrent: {grp.AllowTanner}", replyMarkup: menu); break; case "settanner": grp.AllowTanner = (choice == "true"); Bot.Api.AnswerCallbackQuery(query.Id, $"Tanner as a role set to: {choice}"); Bot.Api.EditMessageText(query.Message.Chat.Id, query.Message.MessageId, $"What would you like to do?", replyMarkup: GetConfigMenu(groupid)); DB.SaveChanges(); break; case "cult": buttons.Add(new InlineKeyboardButton("Allow", $"setcult|{groupid}|true")); buttons.Add(new InlineKeyboardButton("Disallow", $"setcult|{groupid}|false")); buttons.Add(new InlineKeyboardButton("Cancel", $"setcult|{groupid}|cancel")); menu = new InlineKeyboardMarkup(buttons.Select(x => new[] { x }).ToArray()); Bot.Api.EditMessageText(query.Message.Chat.Id, query.Message.MessageId, $"Allow cult as a role option?\nCurrent: {grp.AllowCult}", replyMarkup: menu); break; case "setcult": grp.AllowCult = (choice == "true"); Bot.Api.AnswerCallbackQuery(query.Id, $"Cult as a role set to: {choice}"); Bot.Api.EditMessageText(query.Message.Chat.Id, query.Message.MessageId, $"What would you like to do?", replyMarkup: GetConfigMenu(groupid)); DB.SaveChanges(); break; case "done": Bot.Api.EditMessageText(query.Message.Chat.Id, query.Message.MessageId, "Thank you, have a good day :)"); break; } } catch (Exception ex) { } } }