Example #1
0
        /// <summary>
        /// adds a shout to be scanned for
        /// </summary>
        /// <param name="term">term which triggers the shout</param>
        /// <param name="id">id of video to play</param>
        /// <param name="cooldown">cooldown which has to pass until the video can get triggered again</param>
        /// <param name="starttime">time in seconds when to start playing the video</param>
        /// <param name="endtime">time in seconds when to stop playing the video</param>
        public void AddShout(string term, string id, TimeSpan cooldown, double starttime, double endtime)
        {
            term = term.ToLower();
            if (context.Database.Update <Shout>().Set(s => s.VideoId == id, s => s.Cooldown == cooldown, s => s.StartSeconds == starttime, s => s.EndSeconds == endtime).Where(s => s.Term == term).Execute() == 0)
            {
                context.Database.Insert <Shout>().Columns(s => s.Term, s => s.VideoId, s => s.Cooldown, s => s.StartSeconds, s => s.EndSeconds).Values(term, id, cooldown, starttime, endtime).Execute();
                lock (shoutlock)
                    shouts.Add(new Shout {
                        Term         = term,
                        VideoId      = id,
                        Cooldown     = cooldown,
                        StartSeconds = starttime,
                        EndSeconds   = endtime
                    });
            }

            lock (shoutlock) {
                Shout shout = shouts.FirstOrDefault(s => s.Term == term);
                if (shout != null)
                {
                    shout.VideoId      = id;
                    shout.Cooldown     = cooldown;
                    shout.StartSeconds = starttime;
                    shout.EndSeconds   = endtime;
                }
            }
        }
Example #2
0
        void OnChatMessage(ChatMessage message)
        {
            lock (shoutlock) {
                string messagedata = message.Message.ToLower();
                Shout  shout       = shouts.FirstOrDefault(s => messagedata.StartsWith(s.Term));
                if (shout == null)
                {
                    return;
                }

                if (shout.Cooldown.Ticks > 0)
                {
                    DateTime lasttrigger;
                    lasttriggers.TryGetValue(shout.Term, out lasttrigger);
                    if (DateTime.Now - lasttrigger < shout.Cooldown)
                    {
                        TimeSpan cooldown = shout.Cooldown - (DateTime.Now - lasttrigger);
                        Logger.Warning(this, $"{message.Service}:{message.User} tried to shout '{shout.Term}' but needs to wait another {cooldown.TotalMinutes} minutes to do that.");
                        context.GetModule <StreamModule>().SendMessage(message.Service, message.Channel, message.User, $"You need to wait another {cooldown.TotalMinutes} minutes before shouting '{shout.Term}' again.");
                        return;
                    }

                    lasttriggers[shout.Term] = DateTime.Now;
                }

                context.GetModule <VideoServiceModule>().AddVideo(shout.VideoId, shout.StartSeconds, shout.EndSeconds);
            }
        }