Beispiel #1
0
            static TranslateCommands()
            {
                NadekoBot.Client.MessageReceived += async(msg) =>
                {
                    try
                    {
                        var umsg = msg as SocketUserMessage;
                        if (umsg == null)
                        {
                            return;
                        }

                        bool autoDelete;
                        if (!TranslatedChannels.TryGetValue(umsg.Channel.Id, out autoDelete))
                        {
                            return;
                        }
                        var key = new UserChannelPair()
                        {
                            UserId    = umsg.Author.Id,
                            ChannelId = umsg.Channel.Id,
                        };

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

                        var text = await TranslateInternal(langs, umsg.Resolve(TagHandling.Ignore), true)
                                   .ConfigureAwait(false);

                        if (autoDelete)
                        {
                            try { await umsg.DeleteAsync().ConfigureAwait(false); } catch { }
                        }
                        await umsg.Channel.SendConfirmAsync($"{umsg.Author.Mention} `:` " + text.Replace("<@ ", "<@").Replace("<@! ", "<@!")).ConfigureAwait(false);
                    }
                    catch { }
                };
            }
Beispiel #2
0
            public async Task AutoTranslate(IUserMessage msg, AutoDeleteAutoTranslate autoDelete = AutoDeleteAutoTranslate.Nodel)
            {
                var channel = (ITextChannel)msg.Channel;

                if (autoDelete == AutoDeleteAutoTranslate.Del)
                {
                    TranslatedChannels.AddOrUpdate(channel.Id, true, (key, val) => true);
                    try { await channel.SendMessageAsync("`Started automatic translation of messages on this channel. User messages will be auto-deleted.`").ConfigureAwait(false); } catch { }
                    return;
                }

                bool throwaway;

                if (TranslatedChannels.TryRemove(channel.Id, out throwaway))
                {
                    try { await channel.SendMessageAsync("`Stopped automatic translation of messages on this channel.`").ConfigureAwait(false); } catch { }
                    return;
                }
                else if (TranslatedChannels.TryAdd(channel.Id, autoDelete == AutoDeleteAutoTranslate.Del))
                {
                    try { await channel.SendMessageAsync("`Started automatic translation of messages on this channel.`").ConfigureAwait(false); } catch { }
                }
            }
Beispiel #3
0
        public SearchesService(DiscordSocketClient client, IGoogleApiService google,
                               DbService db, Kotocorn 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);
            };

            //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.");
            }
        }