public static void SetLink(Message msg, string[] args) { if (!String.IsNullOrEmpty(msg.Chat.Username)) { msg.Reply(GetTranslation("SetLinkDone", GetLanguage(msg.Chat.Id), $"<a href='https://t.me/{msg.Chat.Username}'>{msg.Chat.Title.FormatHTML()}</a>")); return; } if (args.Length < 2 || String.IsNullOrEmpty(args[1])) { msg.Reply(GetTranslation("SetLinkNoLink", GetLanguage(msg.Chat.Id))); return; } var link = args[1].Trim(); if (!Regex.IsMatch(link, @"^(https?:\/\/)?t(elegram)?\.me\/joinchat\/([a-zA-Z0-9_\-]+)$")) { msg.Reply(GetTranslation("SetLinkInvalidLink", GetLanguage(msg.Chat.Id))); return; } using (var db = new SixNimmtDb()) { var grp = db.Groups.FirstOrDefault(x => x.GroupId == msg.Chat.Id) ?? MakeDefaultGroup(msg.Chat); grp.GroupLink = link; db.SaveChanges(); } msg.Reply(GetTranslation("SetLinkDone", GetLanguage(msg.Chat.Id), $"<a href=\"{link}\">{msg.Chat.Title.FormatHTML()}</a>")); }
public static void NextGame(Message msg, string[] args) { if (msg.Chat.Type == ChatType.Private) { return; } var grpId = msg.Chat.Id; using (var db = new SixNimmtDb()) { var dbGrp = db.Groups.FirstOrDefault(x => x.GroupId == grpId); if (dbGrp != null) { var notified = db.NotifyGames.FirstOrDefault(x => x.GroupId == grpId && x.UserId == msg.From.Id); if (notified != null) { Bot.Send(msg.From.Id, GetTranslation("AlreadyInWaitingList", GetLanguage(msg.From.Id))); return; } else { } db.Database.ExecuteSqlCommand($"INSERT INTO NotifyGame VALUES ({msg.From.Id}, {msg.Chat.Id})"); db.SaveChanges(); Bot.Send(msg.From.Id, GetTranslation("NextGame", GetLanguage(msg.From.Id))); } } }
public static void RemLink(Message msg, string[] args) { using (var db = new SixNimmtDb()) { var grp = db.Groups.FirstOrDefault(x => x.GroupId == msg.Chat.Id) ?? MakeDefaultGroup(msg.Chat); grp.GroupLink = null; db.SaveChanges(); } msg.Reply(GetTranslation("LinkRemoved", GetLanguage(msg.Chat.Id))); }
public static void SetChooseCardTimeConfig(long chatId, int chooseCardTime) { using (var db = new SixNimmtDb()) { var grp = db.Groups.FirstOrDefault(x => x.GroupId == chatId); if (grp == null) { return; } grp.ChooseCardTime = chooseCardTime; db.SaveChanges(); } }
public static void SetGroupListConfig(long chatId, bool show) { using (var db = new SixNimmtDb()) { var grp = db.Groups.FirstOrDefault(x => x.GroupId == chatId); if (grp == null) { return; } grp.ShowOnGroupList = show; db.SaveChanges(); } }
public static void SetCardDeckConfig(long chatId, bool useDynamic) { using (var db = new SixNimmtDb()) { var grp = db.Groups.FirstOrDefault(x => x.GroupId == chatId); if (grp == null) { return; } grp.DynamicDeck = useDynamic; db.SaveChanges(); } }
public static void SetTableConfig(int userId, bool config) { using (var db = new SixNimmtDb()) { var user = db.Players.FirstOrDefault(x => x.TelegramId == userId); if (user == null) { return; } user.UseSticker = config; db.SaveChanges(); } }
public static void SetLanguage(int userId, string lang) { using (var db = new SixNimmtDb()) { var user = db.Players.FirstOrDefault(x => x.TelegramId == userId); if (user == null) { return; } user.Language = lang; db.SaveChanges(); } }
public static void SetTableConfig(long chatId, bool config) { if (int.TryParse(chatId.ToString(), out int o)) { SetTableConfig(o, config); return; } using (var db = new SixNimmtDb()) { var grp = db.Groups.FirstOrDefault(x => x.GroupId == chatId); if (grp == null) { return; } grp.UseSticker = config; db.SaveChanges(); } }
public static void SetLanguage(long chatId, string lang) { if (int.TryParse(chatId.ToString(), out int o)) { SetLanguage(o, lang); return; } using (var db = new SixNimmtDb()) { var grp = db.Groups.FirstOrDefault(x => x.GroupId == chatId); if (grp == null) { return; } grp.Language = lang; db.SaveChanges(); } }
public static string GetLanguage(long id) { using (var db = new SixNimmtDb()) { Player p = null; var grp = db.Groups.FirstOrDefault(x => x.GroupId == id); if (grp == null) { p = db.Players.FirstOrDefault(x => x.TelegramId == id); } if (p != null && String.IsNullOrEmpty(p.Language)) { p.Language = "English"; db.SaveChanges(); } return(grp?.Language ?? p?.Language ?? "English"); } }
public static void Config(Message msg, string[] args) { var id = msg.Chat.Id; //make sure the group is in the database using (var db = new SixNimmtDb()) { switch (msg.Chat.Type) { case ChatType.Group: case ChatType.Supergroup: var grp = db.Groups.FirstOrDefault(x => x.GroupId == id); if (grp == null) { grp = Helpers.MakeDefaultGroup(msg.Chat); db.Groups.Add(grp); } grp.UserName = msg.Chat.Username; grp.Name = msg.Chat.Title; break; case ChatType.Private: var p = db.Players.FirstOrDefault(x => x.TelegramId == id); if (p == null) { p = Helpers.MakeDefaultPlayer(msg.From); db.Players.Add(p); } p.UserName = msg.From.Username; p.Name = msg.From.FirstName; break; } db.SaveChanges(); } var menu = Handler.GetConfigMenu(msg.Chat.Id); Bot.Send(msg.From.Id, GetTranslation("WhatToDo", GetLanguage(msg.Chat.Id)), replyMarkup: menu); }
public static void ChangeGroupLang(Message msg, string[] args) { if (args == null) { return; } var lang = args[1]; try { using (var db = new SixNimmtDb()) { var p = db.Groups.FirstOrDefault(x => x.GroupId == msg.Chat.Id); if (p != null) { p.Language = lang; db.SaveChanges(); Bot.Send(msg.Chat.Id, "OK"); } } } catch { } }
public static void SetLang(Message msg, string[] args) { var id = msg.From.Id; //make sure the user is in the database using (var db = new SixNimmtDb()) { var user = db.Players.FirstOrDefault(x => x.TelegramId == id); if (user == null) { user = Helpers.MakeDefaultPlayer(msg.From); db.Players.Add(user); } user.UserName = msg.From.Username; user.Name = msg.From.FirstName; db.SaveChanges(); } var menu = Handler.GetConfigLangMenu(msg.From.Id, true); Bot.Send(msg.From.Id, GetTranslation("ChoosePMLanguage", GetLanguage(msg.From.Id)), replyMarkup: menu); }
public static void AddAchv(Message msg, string[] args) { //get the user to add the achievement to //first, try by reply var id = 0; var achIndex = 0; var param = args[1].Split(' '); if (msg.ReplyToMessage != null) { var m = msg.ReplyToMessage; while (m.ReplyToMessage != null) { m = m.ReplyToMessage; } //check for forwarded message id = m.From.Id; if (m.ForwardFrom != null) { id = m.ForwardFrom.Id; } } else { //ok, check for a user mention var e = msg.Entities?.FirstOrDefault(); if (e != null) { switch (e.Type) { case MessageEntityType.Mention: //get user var username = msg.Text.Substring(e.Offset + 1, e.Length - 1); using (var db = new SixNimmtDb()) { id = db.Players.FirstOrDefault(x => x.UserName == username)?.TelegramId ?? 0; } break; case MessageEntityType.TextMention: id = e.User.Id; break; } achIndex = 1; } } if (id == 0) { //check for arguments then if (int.TryParse(param[0], out id)) { achIndex = 1; } else if (int.TryParse(param[1], out id)) { achIndex = 0; } } if (id != 0) { //try to get the achievement if (Enum.TryParse(param[achIndex], out Achievements a)) { //get the player from database using (var db = new SixNimmtDb()) { var p = db.Players.FirstOrDefault(x => x.TelegramId == id); if (p != null) { if (p.Achievements == null) { p.Achievements = 0; } var ach = (Achievements)p.Achievements; if (ach.HasFlag(a)) { msg.Reply("This player already have this achivement!"); return; //no point making another db call if they already have it } ach = ach | a; p.Achievements = (long)ach; db.SaveChanges(); var achvMsg = GetTranslation("NewUnlocks", GetLanguage(p.TelegramId)).ToBold() + Environment.NewLine + Environment.NewLine; achvMsg += $"{a.GetAchvName(GetLanguage(p.TelegramId)).ToBold()}\n{a.GetAchvDescription(GetLanguage(p.TelegramId))}"; Bot.Send(p.TelegramId, achvMsg); msg.Reply($"Achievement {a} unlocked for {p.Name}"); } } } } else { msg.Reply("Please reply to a (forwarded) message of the player, or provide the ID/Username/Mention."); } }
public static void HandleMessage(Message msg) { if (msg.Date.ToUniversalTime() < Program.Startup.ToUniversalTime()) { return; } switch (msg.Type) { case MessageType.TextMessage: string text = msg.Text; string[] args = text.Contains(' ') ? new[] { text.Split(' ')[0].ToLower(), text.Remove(0, text.IndexOf(' ') + 1) } : new[] { text.ToLower(), null }; if (args[0].EndsWith('@' + Bot.Me.Username.ToLower())) { args[0] = args[0].Remove(args[0].Length - Bot.Me.Username.Length - 1); } if (msg.Text.StartsWith("/")) { args[0] = args[0].Substring(1); var cmd = Bot.Commands.FirstOrDefault(x => x.Trigger == args[0]); if (cmd != null) { if (new[] { ChatType.Supergroup, ChatType.Group }.Contains(msg.Chat.Type)) { using (var db = new SixNimmtDb()) { var DbGroup = db.Groups.FirstOrDefault(x => x.GroupId == msg.Chat.Id); if (DbGroup == null) { DbGroup = Helpers.MakeDefaultGroup(msg.Chat); db.Groups.Add(DbGroup); db.SaveChanges(); } } } if (cmd.GroupOnly && !new[] { ChatType.Supergroup, ChatType.Group }.Contains(msg.Chat.Type)) { msg.Reply("This command can only be used in groups!"); return; } if (cmd.AdminOnly && new[] { ChatType.Supergroup, ChatType.Group }.Contains(msg.Chat.Type) && !Helpers.IsGroupAdmin(msg)) { msg.Reply("You aren't a group admin!"); return; } if (cmd.DevOnly && !Constants.Dev.Contains(msg.From.Id)) { msg.Reply("You aren't a bot dev!"); return; } cmd.Method.Invoke(msg, args); return; } } break; case MessageType.ServiceMessage: bool me = false; if (msg.NewChatMembers.Any()) { foreach (var c in msg.NewChatMembers) { if (c.Id == Bot.Me.Id) { me = true; } } } if (msg.NewChatMember != null && msg.NewChatMember.Id == Bot.Me.Id) { me = true; } if (me) { msg.ReplyNoQuote("Thank you for adding me into this chat! [6 Nimmt!] is a board game orininated in Germany, it's meaning is [Take 6!]. " + "It is a easy game to be played by 3 - 10 players. Admins can use /config to do simple configurations. Press /startgame to start a new game now!"); } break; /* * case MessageType.SuccessfulPayment: * var randomRef = Guid.NewGuid().ToString(); * randomRef = $"#crim_{randomRef.Substring(randomRef.Length - 12)}"; * using (var db = new SixNimmtDb()) * { * var donate = new Donation * { * TelegramId = msg.From.Id, * Amount = msg.SuccessfulPayment.TotalAmount / 100, * Reference = randomRef, * DonationTime = DateTime.UtcNow * }; * db.Donations.Add(donate); * db.SaveChanges(); * } * // notify user successful donation, provide reference code for checking in case * msg.Reply(GetTranslation("DonateSuccessful", GetLanguage(msg.From.Id), randomRef)); * // log who, how much, when and ref code to log group * Bot.Send(Constants.LogGroupId, $"Donation from user <a href='tg://user?id={msg.From.Id}'>{msg.From.FirstName.FormatHTML()}</a>.\nAmount: {msg.SuccessfulPayment.TotalAmount / 100} HKD\nReference: {randomRef}"); * break; */ } }