/// <summary>
        /// Check if exists a specified punish.
        /// </summary>
        /// <param name="player">The <see cref="Collections.Player"/> player.</param>
        /// <param name="type"> the <see cref="Enums.PunishType"/>.</param>
        /// <param name="id"> the punish id.</param>
        /// <param name="server"> the server port.</param>
        /// <returns> true if the punish exists, false if not.</returns>
        public static bool CheckId(Collections.Player player, PunishType?type, int id, int server)
        {
            switch (type)
            {
            case PunishType.Ban:
                return(BanCollection.Exists(ban => ban.Target == player && ban.BanId == id && ban.Server == server));

            case PunishType.Kick:
                return(KickCollection.Exists(kick => kick.Target == player && kick.KickId == id && kick.Server == server));

            case PunishType.Mute:
                return(MuteCollection.Exists(mute => mute.Target == player && mute.MuteId == id && mute.Server == server));

            case PunishType.Warn:
                return(WarnCollection.Exists(warn => warn.Target == player && warn.WarnId == id && warn.Server == server));

            case PunishType.SoftWarn:
                return(SoftWarnCollection.Exists(sw => sw.Target == player && sw.SoftWarnId == id && sw.Server == server));

            case PunishType.SoftBan:
                return(SoftBanCollection.Exists(sb => sb.Target == player && sb.SoftBanId == id && sb.Server == server));

            case PunishType.WatchList:
                return(WatchListCollection.Exists(wl => wl.Target == player && wl.WatchListId == id && wl.Server == server));

            default: return(false);
            }
        }
 /// <summary>
 /// Clear all punishment from player <see cref="Player"/>.
 /// </summary>
 /// <param name="player">The <see cref="Collections.Player"/> player.</param>
 public static void Clear(this Collections.Player player)
 {
     BanCollection.DeleteMany(p => p.Target == player);
     MuteCollection.DeleteMany(p => p.Target == player);
     KickCollection.DeleteMany(p => p.Target == player);
     WarnCollection.DeleteMany(p => p.Target == player);
     SoftBanCollection.DeleteMany(p => p.Target == player);
     SoftWarnCollection.DeleteMany(p => p.Target == player);
     WatchListCollection.DeleteMany(p => p.Target == player);
     JsonManager.PunishToCache(PunishType.All, JsonConvert.SerializeObject(player), player, ActionType.Remove);
 }
        public static void Open()
        {
            try
            {
                if (!Directory.Exists(Folder))
                {
                    Directory.CreateDirectory(Folder);
                }

                LiteDatabase        = new LiteDatabase(DatabasePath);
                PlayerCollection    = LiteDatabase.GetCollection <Player>();
                WarnCollection      = LiteDatabase.GetCollection <Warn>();
                KickCollection      = LiteDatabase.GetCollection <Kick>();
                MuteCollection      = LiteDatabase.GetCollection <Mute>();
                BanCollection       = LiteDatabase.GetCollection <Ban>();
                SoftBanCollection   = LiteDatabase.GetCollection <SoftBan>();
                SoftWarnCollection  = LiteDatabase.GetCollection <SoftWarn>();
                WatchListCollection = LiteDatabase.GetCollection <WatchList>();

                PlayerCollection.EnsureIndex(p => p.Id, true);
                WarnCollection.EnsureIndex(w => w.Target.Id);
                WarnCollection.EnsureIndex(w => w.Issuer.Id);
                WarnCollection.EnsureIndex(w => w.Date);
                KickCollection.EnsureIndex(k => k.Target.Id);
                KickCollection.EnsureIndex(k => k.Issuer.Id);
                KickCollection.EnsureIndex(k => k.Date);
                MuteCollection.EnsureIndex(m => m.Target.Id);
                MuteCollection.EnsureIndex(m => m.Issuer.Id);
                MuteCollection.EnsureIndex(m => m.Date);
                MuteCollection.EnsureIndex(m => m.Expire);
                BanCollection.EnsureIndex(b => b.Target.Id);
                BanCollection.EnsureIndex(b => b.Issuer.Id);
                BanCollection.EnsureIndex(b => b.Date);
                BanCollection.EnsureIndex(b => b.Expire);
                SoftBanCollection.EnsureIndex(sb => sb.Target.Id);
                SoftBanCollection.EnsureIndex(sb => sb.Issuer.Id);
                SoftBanCollection.EnsureIndex(sb => sb.Date);
                SoftBanCollection.EnsureIndex(sb => sb.Expire);
                SoftWarnCollection.EnsureIndex(sw => sw.Target.Id);
                SoftWarnCollection.EnsureIndex(sw => sw.Issuer.Id);
                SoftWarnCollection.EnsureIndex(sw => sw.Date);
                WatchListCollection.EnsureIndex(wl => wl.Target.Id);
                WatchListCollection.EnsureIndex(wl => wl.Issuer.Id);
                WatchListCollection.EnsureIndex(wl => wl.Date);

                Log.Info("Database Loaded!");
            }
            catch (Exception e)
            {
                Log.Error($"Error when try to open database:\n {e}");
            }
        }
示例#4
0
        internal static void OnFileChanged(object sender, FileSystemEventArgs e)
        {
            if (!Plugin.Singleton.Config.IsDatabaseGlobal || !Plugin.Singleton.Config.ReceiveFrom.Contains(Path.GetFileNameWithoutExtension(e.Name)?.Split('-')[4]) || Path.GetFileNameWithoutExtension(e.Name)?.Split('-')[4] == Server.Port.ToString())
            {
                return;
            }

            string playerId    = Path.GetFileNameWithoutExtension(e.Name)?.Split('-')[2];
            string rawPlayerId = playerId?.Split('@')[0];

            if (!PlayerCollection.Exists(p => p.Id == rawPlayerId))
            {
                Player player = new Player
                                (
                    rawPlayerId,
                    playerId?.Split('@')[1],
                    e.Name?.Split('-')[3]
                                );
                player.Save();
            }

            switch (Path.GetFileNameWithoutExtension(e.Name)?.Split('-')[1].GetPunishType())
            {
            case PunishType.Ban:
                Ban ban = Utf8Json.JsonSerializer.Deserialize <Ban>(File.ReadAllText(e.FullPath));

                if (ban != null && !ban.Clear)
                {
                    new Ban(ban.Target, ban.Issuer, ban.Reason, ban.Duration, ban.Date, ban.Expire, ban.BanId,
                            ban.Server, false).Save();
                    break;
                }

                BanCollection.DeleteMany(b =>
                                         b.Target == ban.Target && b.BanId == ban.BanId && b.Server == ban.Server);
                break;

            case PunishType.Kick:
                Kick kick = Utf8Json.JsonSerializer.Deserialize <Kick>(File.ReadAllText(e.FullPath));

                if (kick != null && !kick.Clear)
                {
                    new Kick(kick.Target, kick.Issuer, kick.Reason, kick.Date, kick.KickId, kick.Server,
                             false).Save();
                    break;
                }

                KickCollection.DeleteMany(k =>
                                          k.Target == kick.Target && k.KickId == kick.KickId && k.Server == kick.Server);
                break;

            case PunishType.Mute:
                Mute mute = Utf8Json.JsonSerializer.Deserialize <Mute>(File.ReadAllText(e.FullPath));

                if (mute != null && !mute.Clear)
                {
                    new Mute(mute.Target, mute.Issuer, mute.Reason, mute.Duration, mute.Date, mute.Expire,
                             mute.MuteId, mute.Server, false).Save();
                    break;
                }

                KickCollection.DeleteMany(m =>
                                          m.Target == mute.Target && m.KickId == mute.MuteId && m.Server == mute.Server);
                break;

            case PunishType.Warn:
                Warn warn = Utf8Json.JsonSerializer.Deserialize <Warn>(File.ReadAllText(e.FullPath));

                if (warn != null && !warn.Clear)
                {
                    new Warn(warn.Target, warn.Issuer, warn.Reason, warn.Date, warn.WarnId, warn.Server,
                             false).Save();
                    break;
                }

                WarnCollection.DeleteMany(w =>
                                          w.Target == warn.Target && w.WarnId == warn.WarnId && w.Server == warn.Server);
                break;

            case PunishType.SoftBan:
                SoftBan softBan = Utf8Json.JsonSerializer.Deserialize <SoftBan>(File.ReadAllText(e.FullPath));

                if (softBan != null && !softBan.Clear)
                {
                    new SoftBan(softBan.Target, softBan.Issuer, softBan.Reason, softBan.Duration, softBan.Date,
                                softBan.Expire, softBan.SoftBanId, softBan.Server, false).Save();
                    break;
                }

                SoftBanCollection.DeleteMany(sb =>
                                             sb.Target == softBan.Target && sb.SoftBanId == softBan.SoftBanId &&
                                             sb.Server == softBan.Server);
                break;

            case PunishType.SoftWarn:
                SoftWarn softWan = Utf8Json.JsonSerializer.Deserialize <SoftWarn>(File.ReadAllText(e.FullPath));

                if (softWan != null && !softWan.Clear)
                {
                    new SoftWarn(softWan.Target, softWan.Issuer, softWan.Reason, softWan.Date, softWan.SoftWarnId,
                                 softWan.Server, false).Save();
                    break;
                }

                SoftWarnCollection.DeleteMany(sw =>
                                              sw.Target == softWan.Target && sw.SoftWarnId == softWan.SoftWarnId &&
                                              sw.Server == softWan.Server);
                break;

            case PunishType.WatchList:
                WatchList watchList = JsonConvert.DeserializeObject <WatchList>(File.ReadAllText(e.FullPath));

                if (watchList != null && !watchList.Clear)
                {
                    new WatchList(watchList.Target, watchList.Issuer, watchList.Reason, watchList.Date,
                                  watchList.WatchListId, watchList.Server, false).Save();
                    break;
                }

                WatchListCollection.DeleteMany(wl =>
                                               wl.Target == watchList.Target && wl.WatchListId == watchList.WatchListId &&
                                               wl.Server == watchList.Server);
                break;

            case PunishType.All:
                JsonConvert.DeserializeObject <Player>(File.ReadAllText(e.FullPath)).Clear();
                break;
            }
            File.Delete(e.FullPath);
        }
        /// <summary>
        /// Apply a punish to the specified <see cref="Player"/>.
        /// </summary>
        /// <param name="target">The <see cref="Exiled.API.Features.Player"/> player.</param>
        /// <param name="issuer">The <see cref="Collections.Player"/> staffer.</param>
        /// <param name="dPlayer">The <see cref="Collections.Player"/> player.</param>
        /// <param name="punishType">The <see cref="Enums.PunishType"/> punish.</param>
        /// <param name="reason">The reason of the punish.</param>
        /// <param name="duration">The <see cref="DateTime"/> duration.</param>
        public static void ApplyPunish(Player target, Collections.Player issuer, Collections.Player dPlayer, PunishType punishType, string reason, string duration)
        {
            switch (punishType)
            {
            case PunishType.Warn:
                Warn warn = new Warn(dPlayer, issuer, reason, DateTime.Now, WarnCollection.Find(w => w.Target.Id == dPlayer.Id).Count(), Server.Port, false);
                warn.Save();

                target?.Broadcast(Plugin.Singleton.Config.Translation.WarnTranslation.PlayerWarnedMessage.Duration,
                                  Plugin.Singleton.Config.Translation.WarnTranslation.PlayerWarnedMessage.Content.Replace(
                                      "{reason}", reason), global::Broadcast.BroadcastFlags.Normal, true);

                JsonManager.PunishToCache(punishType, JsonConvert.SerializeObject(warn), dPlayer, ActionType.Add);
                break;

            case PunishType.Mute:
                Mute mute = new Mute(dPlayer, issuer, reason, GetDate(duration), DateTime.Now, DateTime.Now.AddSeconds(GetTotalSeconds(duration).TotalSeconds), MuteCollection.Find(m => m.Target == dPlayer).Count(), Server.Port, false);
                mute.Save();

                MuteHandler.IssuePersistentMute(dPlayer.Id + dPlayer.Authentication);
                target?.Broadcast(Plugin.Singleton.Config.Translation.MuteTranslation.PlayerMuteMessage.Duration,
                                  Plugin.Singleton.Config.Translation.WarnTranslation.PlayerWarnedMessage.Content
                                  .Replace("{duration}", GetDate(duration)).Replace("{reason}", reason));

                JsonManager.PunishToCache(punishType, JsonConvert.SerializeObject(mute), dPlayer, ActionType.Add);
                break;

            case PunishType.Kick:
                Kick kick = new Kick(dPlayer, issuer, reason, DateTime.Now, KickCollection.Find(x => x.Target.Id == dPlayer.Id).Count(), Server.Port, false);
                kick.Save();

                target?.Kick(Plugin.Singleton.Config.Translation.KickTranslation.PlayerKickedMessage.Replace("{reason}", reason));

                JsonManager.PunishToCache(punishType, JsonConvert.SerializeObject(kick), dPlayer, ActionType.Add);
                break;

            case PunishType.Ban:
                Ban ban = new Ban(dPlayer, issuer, reason, GetDate(duration, true), DateTime.Now, DateTime.Now.AddSeconds(GetTotalSeconds(duration, true).TotalSeconds), BanCollection.Find(x => x.Target.Id == dPlayer.Id).Count(), Server.Port, false);
                ban.Save();
                target?.Disconnect(Plugin.Singleton.Config.Translation.BanTranslation.PlayerBanMessage.Replace("{reason}", reason));
                JsonManager.PunishToCache(punishType, JsonConvert.SerializeObject(ban), dPlayer, ActionType.Add);
                break;

            case PunishType.SoftWarn:
                SoftWarn softWarn = new SoftWarn(dPlayer, issuer, reason, DateTime.Now, SoftWarnCollection.Find(sw => sw.Target.Id == dPlayer.Id).Count(), Server.Port, false);
                softWarn.Save();

                target?.Broadcast(
                    Plugin.Singleton.Config.Translation.SoftWarnTranslation.PlayerSoftWarnedMessage.Duration,
                    Plugin.Singleton.Config.Translation.SoftWarnTranslation.PlayerSoftWarnedMessage.Content.Replace(
                        "{reason}", reason));

                JsonManager.PunishToCache(punishType, JsonConvert.SerializeObject(softWarn), dPlayer,
                                          ActionType.Add);
                break;

            case PunishType.SoftBan:
                SoftBan softBan = new SoftBan(dPlayer, issuer, reason, GetDate(duration), DateTime.Now, DateTime.Now.AddSeconds(GetTotalSeconds(duration).TotalSeconds), SoftBanCollection.Find(sb => sb.Target.Id == dPlayer.Id).Count(), Server.Port, false);
                softBan.Save();

                target?.Broadcast(
                    Plugin.Singleton.Config.Translation.SoftBanTranslation.PlayerSoftBanMessage.Duration,
                    Plugin.Singleton.Config.Translation.StaffTranslation.StaffSoftBanMessage.Content.Replace(
                        "{duration}",
                        GetDate(duration).Replace("{reason}", reason)));

                if (target != null && target.Role != RoleType.Spectator)
                {
                    target.Role.Type = RoleType.Spectator;
                }

                JsonManager.PunishToCache(punishType, JsonConvert.SerializeObject(softBan), dPlayer, ActionType.Add);
                break;

            case PunishType.WatchList:
                WatchList watchList = new WatchList(dPlayer, issuer, reason, DateTime.Now, WatchListCollection.Find(wl => wl.Target.Id == dPlayer.Id).Count(), Server.Port, false);
                watchList.Save();

                JsonManager.PunishToCache(punishType, JsonConvert.SerializeObject(watchList), dPlayer, ActionType.Add);
                break;
            }

            if (punishType is PunishType.WatchList)
            {
                Timing.RunCoroutine(DiscordHandler.SendMessage(Plugin.Singleton.Config.Translation.DiscordTranslation.MessageContentWatchlist.Replace("{target}", $"{dPlayer.Name} ({dPlayer.Id}@{dPlayer.Authentication})").Replace("{reason}", reason).Replace("{issuer}", $"{issuer.Name} ({issuer.Id}@{issuer.Authentication})"), Plugin.Singleton.Config.Translation.DiscordTranslation.WebhookUrlWatchlist));
            }

            Timing.RunCoroutine(DiscordHandler.SendMessage(Plugin.Singleton.Config.Translation.DiscordTranslation.MessageContent.Replace("{target}", $"{dPlayer.Name} ({dPlayer.Id}@{dPlayer.Authentication})").Replace("{reason}", reason).Replace("{action}", punishType.ToString()).Replace("{issuer}", $"{issuer.Name} ({issuer.Id}@{issuer.Authentication})").Replace("{duration}", GetDate(duration, true)), Plugin.Singleton.Config.Translation.DiscordTranslation.WebhookUrl));
        }
        /// <summary>
        /// Clear a punish to the specified <see cref="Player"/>.
        /// </summary>
        /// <param name="player">The <see cref="Collections.Player"/> player.</param>
        /// <param name="type"> the <see cref="Enums.PunishType"/>.</param>
        /// <param name="id"> the punish id.</param>
        /// <param name="server"> the server port.</param>
        public static void ClearPunishment(Collections.Player player, PunishType?type, int id, int server)
        {
            switch (type)
            {
            case PunishType.Ban:
                Ban ban = BanCollection.FindOne(b => b.Target == player && b.BanId == id && b.Server == server);

                JsonManager.PunishToCache(type, JsonConvert.SerializeObject(ban), player, ActionType.Remove);

                if (player.IsBanned())
                {
                    BanHandler.RemoveBan($"{player.Id}@{player.Authentication}", BanHandler.BanType.IP);
                }

                BanCollection.Delete(ban.Id);
                break;

            case PunishType.Kick:
                Kick kick = KickCollection.FindOne(k => k.Target == player && k.KickId == id && k.Server == server);

                JsonManager.PunishToCache(type, JsonConvert.SerializeObject(kick), player, ActionType.Remove);

                KickCollection.Delete(kick.Id);
                break;

            case PunishType.Mute:
                Mute mute = MuteCollection.FindOne(m => m.Target == player && m.MuteId == id && m.Server == server);

                JsonManager.PunishToCache(type, JsonConvert.SerializeObject(mute), player, ActionType.Remove);

                if (player.IsMuted())
                {
                    MuteHandler.RevokePersistentMute($"{player.Id}@{player.Authentication}");
                }

                MuteCollection.Delete(mute.Id);
                break;

            case PunishType.Warn:
                Warn warn = WarnCollection.FindOne(w => w.Target == player && w.WarnId == id && w.Server == server);

                JsonManager.PunishToCache(type, JsonConvert.SerializeObject(warn), player, ActionType.Remove);

                WarnCollection.Delete(warn.Id);
                break;

            case PunishType.SoftWarn:
                SoftWarn softWarn = SoftWarnCollection.FindOne(sw => sw.Target == player && sw.SoftWarnId == id && sw.Server == server);

                JsonManager.PunishToCache(type, JsonConvert.SerializeObject(softWarn), player, ActionType.Remove);

                SoftWarnCollection.Delete(softWarn.Id);
                break;

            case PunishType.SoftBan:
                SoftBan softBan = SoftBanCollection.FindOne(sb => sb.Target == player && sb.SoftBanId == id && sb.Server == server);

                JsonManager.PunishToCache(type, JsonConvert.SerializeObject(softBan), player, ActionType.Remove);

                SoftBanCollection.Delete(softBan.Id);
                break;

            case PunishType.WatchList:
                WatchList watchList = WatchListCollection.FindOne(wl => wl.Target == player && wl.WatchListId == id && wl.Server == server);

                JsonManager.PunishToCache(type, JsonConvert.SerializeObject(watchList), player, ActionType.Remove);

                WatchListCollection.Delete(watchList.Id);
                break;
            }
        }