Beispiel #1
0
 /// <summary>
 /// Resets the counter
 /// </summary>
 /// <param name="e"></param>
 /// <param name="message"></param>
 private void ResetCounter(ServerMessage e, string message)
 {
     lock (_lock)
     {
         Emotes.Clear();
         Emotes.persist();
         LastResetDate = new DateTimeOffset(DateTime.Now).ToString();
         server.fileCommand(_resetDateFile, x => System.IO.File.WriteAllText(x, LastResetDate));
         server.safeSendMessage(e.Channel, "Counter reset successfully!");
     }
 }
Beispiel #2
0
        /// <summary>
        /// Default constructor
        /// </summary>
        /// <param name="_server"></param>
        public EmoteModule(VirtualServer _server) : base(_server, "emote")
        {
            Emotes = PersistantDict <ulong, EmoteStats> .load(_server, "EmoteStats.xml");

            // Get the last reset date
            var res = server.FileSetup(_resetDateFile);

            if (res.Count > 0)
            {
                LastResetDate = res[0];
            }
            else
            {
                _lastResetDate = new DateTimeOffset(DateTime.Now).ToString();
                server.fileCommand(_resetDateFile, x => System.IO.File.WriteAllText(x, LastResetDate));
            }

            Action <Emote> f = (emote) =>
            {
                // Create a new entry if it doesn't have one
                if (!Emotes.ContainsKey(emote.Id))
                {
                    Emotes.Add(emote.Id, new EmoteStats()
                    {
                        Counter    = 0,
                        LastPosted = new DateTimeOffset(DateTime.Now).ToString(),
                        Name       = emote.Name
                    });
                }

                // Increment the counter
                Emotes[emote.Id].Counter++;
                Emotes[emote.Id].LastPosted = new DateTimeOffset(DateTime.Now).ToString();
                Emotes.persist();
            };

            // Listen to all incoming messages from the server
            server.MessageRecieved += (s, e) =>
            {
                if (!on)
                {
                    return;
                }

                List <Emote> usedEmotes = new List <Emote>();

                if (!e.Author.IsBot && e.msg.Tags.Any((t) => t.Type == TagType.Emoji && server.getServer().Emotes.Contains(t.Value)))
                {
                    lock (_lock)
                    {
                        // Go over all the server emotes that weren't already posted
                        foreach (var tag in e.msg.Tags.Where(t => t.Type == TagType.Emoji && server.getServer().Emotes.Contains(t.Value) && !usedEmotes.Contains(t.Value)))
                        {
                            // Add it to the previously posted list
                            Emote emote = tag.Value as Emote;
                            usedEmotes.Add(emote);

                            if (emote != null)
                            {
                                f(emote);
                            }
                        }
                    }
                }
            };

            // Count for reactions
            server.ReactionAdded += (s, reaction) =>
            {
                if (!on)
                {
                    return;
                }

                // If a real user reacted wit ha server emote
                if (!reaction.User.Value.IsBot && server.getServer().Emotes.Contains(reaction.Emote))
                {
                    Emote emote = reaction.Emote as Emote;
                    if (emote != null)
                    {
                        lock (_lock)
                        {
                            f(emote);
                        }
                    }
                }
            };

            server.ReactionRemoved += (s, reaction) =>
            {
                if (!on)
                {
                    return;
                }

                // If a real user removed their server emote reaction
                if (!reaction.User.Value.IsBot && server.getServer().Emotes.Contains(reaction.Emote))
                {
                    Emote emote = reaction.Emote as Emote;
                    if (emote != null)
                    {
                        lock (_lock)
                        {
                            // Decrement the counter
                            if (Emotes.ContainsKey(emote.Id))
                            {
                                Emotes[emote.Id].Counter--;
                                Emotes.persist();
                            }
                        }
                    }
                }
            };
        }