Example #1
0
        public SearchesService(DiscordSocketClient client, IGoogleApiService google,
                               DbService db, NadekoBot bot, IDataCache cache,
                               FontProvider fonts)
        {
            Http = new HttpClient();
            Http.AddFakeHeaders();
            _client = client;
            _google = google;
            _db     = db;
            _log    = LogManager.GetCurrentClassLogger();
            _imgs   = cache.LocalImages;
            _cache  = cache;
            _fonts  = fonts;
            http    = new HttpClient();

            _blacklistedTags = new ConcurrentDictionary <ulong, HashSet <string> >(
                bot.AllGuildConfigs.ToDictionary(
                    x => x.GuildId,
                    x => new HashSet <string>(x.NsfwBlacklistedTags.Select(y => y.Tag))));

            //translate commands
            _client.MessageReceived += (msg) =>
            {
                var _ = Task.Run(async() =>
                {
                    try
                    {
                        var umsg = msg as SocketUserMessage;
                        if (umsg == null)
                        {
                            return;
                        }

                        if (!TranslatedChannels.TryGetValue(umsg.Channel.Id, out var autoDelete))
                        {
                            return;
                        }

                        var key = new UserChannelPair()
                        {
                            UserId    = umsg.Author.Id,
                            ChannelId = umsg.Channel.Id,
                        };

                        if (!UserLanguages.TryGetValue(key, out string langs))
                        {
                            return;
                        }

                        var text = await Translate(langs, umsg.Resolve(TagHandling.Ignore))
                                   .ConfigureAwait(false);
                        if (autoDelete)
                        {
                            try { await umsg.DeleteAsync().ConfigureAwait(false); } catch { }
                        }
                        await umsg.Channel.SendConfirmAsync($"{umsg.Author.Mention} `:` " + text.Replace("<@ ", "<@").Replace("<@! ", "<@!")).ConfigureAwait(false);
                    }
                    catch { }
                });
                return(Task.CompletedTask);
            };

            //pokemon commands
            if (File.Exists(PokemonListFile))
            {
                Pokemons = JsonConvert.DeserializeObject <Dictionary <string, SearchPokemon> >(File.ReadAllText(PokemonListFile));
            }
            else
            {
                _log.Warn(PokemonListFile + " is missing. Pokemon abilities not loaded.");
            }
            if (File.Exists(PokemonAbilitiesFile))
            {
                PokemonAbilities = JsonConvert.DeserializeObject <Dictionary <string, SearchPokemonAbility> >(File.ReadAllText(PokemonAbilitiesFile));
            }
            else
            {
                _log.Warn(PokemonAbilitiesFile + " is missing. Pokemon abilities not loaded.");
            }

            //joke commands
            if (File.Exists("data/wowjokes.json"))
            {
                WowJokes = JsonConvert.DeserializeObject <List <WoWJoke> >(File.ReadAllText("data/wowjokes.json"));
            }
            else
            {
                _log.Warn("data/wowjokes.json is missing. WOW Jokes are not loaded.");
            }

            if (File.Exists("data/magicitems.json"))
            {
                MagicItems = JsonConvert.DeserializeObject <List <MagicItem> >(File.ReadAllText("data/magicitems.json"));
            }
            else
            {
                _log.Warn("data/magicitems.json is missing. Magic items are not loaded.");
            }
        }
Example #2
0
        public SearchesService(DiscordSocketClient client, IGoogleApiService google,
                               DbService db, NadekoBot bot, IDataCache cache,
                               FontProvider fonts)
        {
            Http = new HttpClient();
            Http.AddFakeHeaders();
            _client = client;
            _google = google;
            _db     = db;
            _log    = LogManager.GetCurrentClassLogger();
            _imgs   = cache.LocalImages;
            _cache  = cache;
            _fonts  = fonts;

            _blacklistedTags = new ConcurrentDictionary <ulong, HashSet <string> >(
                bot.AllGuildConfigs.ToDictionary(
                    x => x.GuildId,
                    x => new HashSet <string>(x.NsfwBlacklistedTags.Select(y => y.Tag))));

            //translate commands
            _client.MessageReceived += (msg) =>
            {
                var _ = Task.Run(async() =>
                {
                    try
                    {
                        var umsg = msg as SocketUserMessage;
                        if (umsg == null)
                        {
                            return;
                        }

                        if (!TranslatedChannels.TryGetValue(umsg.Channel.Id, out var autoDelete))
                        {
                            return;
                        }

                        var key = new UserChannelPair()
                        {
                            UserId    = umsg.Author.Id,
                            ChannelId = umsg.Channel.Id,
                        };

                        if (!UserLanguages.TryGetValue(key, out string langs))
                        {
                            return;
                        }

                        var text = await Translate(langs, umsg.Resolve(TagHandling.Ignore))
                                   .ConfigureAwait(false);
                        if (autoDelete)
                        {
                            try { await umsg.DeleteAsync().ConfigureAwait(false); } catch { }
                        }
                        await umsg.Channel.SendConfirmAsync($"{umsg.Author.Mention} `:` " + text.Replace("<@ ", "<@").Replace("<@! ", "<@!")).ConfigureAwait(false);
                    }
                    catch { }
                });
                return(Task.CompletedTask);
            };

            if (client.ShardId == 0)
            {
                _t = new Timer(async _ =>
                {
                    var r = _cache.Redis.GetDatabase();
                    try
                    {
                        var data = (string)(await r.StringGetAsync("crypto_data").ConfigureAwait(false));
                        if (data == null)
                        {
                            data = await Http.GetStringAsync("https://api.coinmarketcap.com/v1/ticker/")
                                   .ConfigureAwait(false);

                            await r.StringSetAsync("crypto_data", data, TimeSpan.FromHours(6)).ConfigureAwait(false);
                        }
                    }
                    catch (Exception ex)
                    {
                        _log.Warn(ex);
                    }
                }, null, TimeSpan.Zero, TimeSpan.FromHours(1));
            }

            //joke commands
            if (File.Exists("data/wowjokes.json"))
            {
                WowJokes = JsonConvert.DeserializeObject <List <WoWJoke> >(File.ReadAllText("data/wowjokes.json"));
            }
            else
            {
                _log.Warn("data/wowjokes.json is missing. WOW Jokes are not loaded.");
            }

            if (File.Exists("data/magicitems.json"))
            {
                MagicItems = JsonConvert.DeserializeObject <List <MagicItem> >(File.ReadAllText("data/magicitems.json"));
            }
            else
            {
                _log.Warn("data/magicitems.json is missing. Magic items are not loaded.");
            }
        }