Example #1
0
        internal static IChannel CreateFromJson(ChannelJson json, [CanBeNull] object state)
        {
            IChannel res = null;

            if (_typeCaches.ContainsKey(json.type))
            {
                _globalCache.Mutex(() => {
                    ICache <IChannel> cache = _typeCaches[json.type];
                    cache.Mutex(() => {
                        if (cache.Contains(json.id))
                        {
                            res = cache[json.id];
                        }
                        else if (_globalCache.Contains(json.id))
                        {
                            throw new WrongChannelTypeException("JSON specifies type of channel is " + json.type + ", but cached data says it should be " + _globalCache[json.id].Type + ".");
                        }
                        else
                        {
                            switch (json.type)
                            {
                            case ChannelType.DirectMessage:
                            case ChannelType.GroupDirectMessage:
                                res = new DirectMessageTextChannel(json, json.id);
                                TextChannel.Populate((TextChannel)res, json, state);
                                break;

                            case ChannelType.ServerCategory:
                                res = new ChannelCategory(json.id, json.id);
                                ChannelCategory.Populate((ChannelCategory)res, json, state);
                                break;

                            case ChannelType.ServerText:
                                res = new ServerTextChannel(json, json.id);
                                TextChannel.Populate((TextChannel)res, json, state);
                                break;
                            }
                            _globalCache.Add(res);
                            cache.Add(res);
                        }
                    });
                });
            }
            return(res);
        }
Example #2
0
        internal static Message Create(JObject obj)
        {
            JsonObjectReader r  = obj.GetReader();
            ulong            id = r.ReadSnowflake("id");

            return(_cache.Mutex(() => {
                Message res;
                if (_cache.Contains(id))
                {
                    res = _cache[id];
                    res.UpdateInstance(r);
                }
                else
                {
                    res = r.Contains("webhook_id") ? (Message) new WebhookMessage(r) : new StandardMessage(r);
                }
                return res;
            }));
        }