Esempio n. 1
0
        internal static string SubstituteEmotes(string msg, SocketGuild guild)
        {
            var emote = new Regex(@"\[(\w+)\]", RegexOptions.Compiled);

            return(emote.Replace(msg, (match) =>
            {
                var symbol = match.Groups[1].Value;
                if (Emotes.ContainsKey(symbol))
                {
                    symbol = Emotes[symbol];
                }

                if (symbol.StartsWith(":"))
                {
                    return symbol;
                }

                var found = FindEmote(symbol, guild);
                if (!string.IsNullOrEmpty(found))
                {
                    return found;
                }

                return $"[{match.Value}]";
            }
                                 ));
        }
Esempio n. 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();
                            }
                        }
                    }
                }
            };
        }