public async Task <bool> AddAsync(TShockAPI.DB.UserAccount user, DateTime expiration) { return(await Task.Run(() => { syncLock.EnterWriteLock(); try { return db.Query("INSERT INTO Mutes VALUES (@0, @1, @2, @3, @4, @5)", null, user.Name, user.UUID, JsonConvert.DeserializeObject <List <string> >(user.KnownIps)[0], DateTime.UtcNow.ToString("s"), expiration.ToString("s")) > 0; } catch (Exception ex) { TShock.Log.Error(ex.ToString()); return false; } finally { syncLock.ExitWriteLock(); } })); }
public bool ChangeGroup(object Player, object Group) { TShockAPI.TSPlayer p = null; TShockAPI.DB.UserAccount u = new TShockAPI.DB.UserAccount(); string g = ""; if ((p = GetPlayer(Player)) == null) { return(false); } if (Group is string) { g = Group as string; } else if (Group is TShockAPI.Group) { g = (Group as TShockAPI.Group).Name; } if (string.IsNullOrEmpty(g) == true) { return(false); } try { u.Name = p.Account.Name; TShockAPI.TShock.UserAccounts.SetUserGroup(u, g); } catch (Exception ex) { ScriptLog.ErrorFormat("tshock_change_group", "Group change failed: {0}", ex.Message); return(false); } return(true); }
public async Task <bool> DeleteAsync(TShockAPI.DB.UserAccount user) { return(await Task.Run(() => { syncLock.EnterWriteLock(); string query = db.GetSqlType() == SqlType.Mysql ? "DELETE FROM Mutes WHERE UUID = @0 OR IP = @1 ORDER BY ID DESC LIMIT 1" : "DELETE FROM Mutes WHERE ID IN (SELECT ID FROM Mutes WHERE UUID = @0 OR IP = @1 ORDER BY ID DESC LIMIT 1)"; try { return db.Query(query, user.UUID, JsonConvert.DeserializeObject <List <string> >(user.KnownIps)[0]) > 0; } catch (Exception ex) { TShock.Log.Error(ex.ToString()); return false; } finally { syncLock.ExitWriteLock(); } })); }
public static async void Mute(CommandArgs e) { string subCmd = e.Parameters.FirstOrDefault() ?? "help"; switch (subCmd.ToLowerInvariant()) { #region Add case "add": { var regex = new Regex(@"^\w+ \w+ (?:""(.+?)""|([^\s]+?))(?: (.+))?$"); Match match = regex.Match(e.Message); if (!match.Success) { e.Player.SendErrorMessage("Invalid syntax! Proper syntax: /mute add <name> [time]"); return; } int seconds = Int32.MaxValue / 1000; if (!String.IsNullOrWhiteSpace(match.Groups[3].Value) && (!TShock.Utils.TryParseTime(match.Groups[3].Value, out seconds) || seconds <= 0 || seconds > Int32.MaxValue / 1000)) { e.Player.SendErrorMessage("Invalid time '{0}'!", match.Groups[3].Value); return; } string playerName = String.IsNullOrWhiteSpace(match.Groups[2].Value) ? match.Groups[1].Value : match.Groups[2].Value; List <TSPlayer> players = TShockAPI.TSPlayer.FindByNameOrID(playerName); if (players.Count == 0) { TShockAPI.DB.UserAccount user = TShock.UserAccounts.GetUserAccountByName(playerName); if (user == null) { e.Player.SendErrorMessage("Invalid player or account '{0}'!", playerName); } else { if (TShock.Groups.GetGroupByName(user.Group).GetDynamicPermission(Permissions.Mute) >= e.Player.Group.GetDynamicPermission(Permissions.Mute)) { e.Player.SendErrorMessage("You can't mute {0}!", user.Name); return; } if (await EssentialsPlus.Mutes.AddAsync(user, DateTime.UtcNow.AddSeconds(seconds))) { TSPlayer.All.SendInfoMessage("{0} muted {1}.", e.Player.Name, user.Name); } else { e.Player.SendErrorMessage("Could not mute, check logs for details."); } } } else if (players.Count > 1) { e.Player.SendErrorMessage("More than one player matched: {0}", String.Join(", ", players.Select(p => p.Name))); } else { if (players[0].Group.GetDynamicPermission(Permissions.Mute) >= e.Player.Group.GetDynamicPermission(Permissions.Mute)) { e.Player.SendErrorMessage("You can't mute {0}!", players[0].Name); return; } if (await EssentialsPlus.Mutes.AddAsync(players[0], DateTime.UtcNow.AddSeconds(seconds))) { TSPlayer.All.SendInfoMessage("{0} muted {1}.", e.Player.Name, players[0].Name); players[0].mute = true; try { await Task.Delay(TimeSpan.FromSeconds(seconds), players[0].GetPlayerInfo().MuteToken); players[0].mute = false; players[0].SendInfoMessage("You have been unmuted."); } catch (TaskCanceledException) { } } else { e.Player.SendErrorMessage("Could not mute, check logs for details."); } } } return; #endregion #region Delete case "del": case "delete": { var regex = new Regex(@"^\w+ \w+ (?:""(.+?)""|([^\s]*?))$"); Match match = regex.Match(e.Message); if (!match.Success) { e.Player.SendErrorMessage("Invalid syntax! Proper syntax: /mute del <name>"); return; } string playerName = String.IsNullOrWhiteSpace(match.Groups[2].Value) ? match.Groups[1].Value : match.Groups[2].Value; List <TSPlayer> players = TShockAPI.TSPlayer.FindByNameOrID(playerName); if (players.Count == 0) { TShockAPI.DB.UserAccount user = TShock.UserAccounts.GetUserAccountByName(playerName); if (user == null) { e.Player.SendErrorMessage("Invalid player or account '{0}'!", playerName); } else { if (await EssentialsPlus.Mutes.DeleteAsync(user)) { TSPlayer.All.SendInfoMessage("{0} unmuted {1}.", e.Player.Name, user.Name); } else { e.Player.SendErrorMessage("Could not unmute, check logs for details."); } } } else if (players.Count > 1) { e.Player.SendErrorMessage("More than one player matched: {0}", String.Join(", ", players.Select(p => p.Name))); } else { if (await EssentialsPlus.Mutes.DeleteAsync(players[0])) { players[0].mute = false; TSPlayer.All.SendInfoMessage("{0} unmuted {1}.", e.Player.Name, players[0].Name); } else { e.Player.SendErrorMessage("Could not unmute, check logs for details."); } } } return; #endregion #region Help default: e.Player.SendSuccessMessage("Mute Sub-Commands:"); e.Player.SendInfoMessage("add <name> [time] - Mutes a player or account."); e.Player.SendInfoMessage("del <name> - Unmutes a player or account."); return; #endregion } }