<commit_before>﻿using Discord;
using Discord.Commands;
using NadekoBot.Attributes;
using NadekoBot.Services;
using NadekoBot.Services.Database;
using Newtonsoft.Json;
using NLog;
using Services.CleverBotApi;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace NadekoBot.Modules.Games
{
    public partial class Games
    {
        [Group]
        public class CleverBotCommands
        {
            private static Logger _log { get; }

            class CleverAnswer {
                public string Status { get; set; }
                public string Response { get; set; }
            }
            //user#discrim is the key
            public static ConcurrentHashSet<string> ChannelsInConversation { get; } = new ConcurrentHashSet<string>();
            public static ConcurrentDictionary<ulong, ChatterBotSession> CleverbotGuilds { get; } = new ConcurrentDictionary<ulong, ChatterBotSession>();

            static CleverBotCommands()
            {
                _log = LogManager.GetCurrentClassLogger();

                using (var uow = DbHandler.UnitOfWork())
                {
                    var bot = ChatterBotFactory.Create(ChatterBotType.CLEVERBOT);
                    CleverbotGuilds = new ConcurrentDictionary<ulong, ChatterBotSession>(
                        uow.GuildConfigs.GetAll()
                            .Where(gc => gc.CleverbotEnabled)
                            .ToDictionary(gc => gc.GuildId, gc => bot.CreateSession()));
                }
            }

            public static async Task<bool> TryAsk(IUserMessage msg) {
                var channel = msg.Channel as ITextChannel;

                if (channel == null)
                    return false;

                ChatterBotSession cleverbot;
                if (!CleverbotGuilds.TryGetValue(channel.Guild.Id, out cleverbot))
                    return false;

                var nadekoId = NadekoBot.Client.GetCurrentUser().Id;
                var normalMention = $"<@{nadekoId}> ";
                var nickMention = $"<@!{nadekoId}> ";
                string message;
                if (msg.Content.StartsWith(normalMention))
                {
                    message = msg.Content.Substring(normalMention.Length).Trim();
                }
                else if (msg.Content.StartsWith(nickMention))
                {
                    message = msg.Content.Substring(nickMention.Length).Trim();
                }
                else
                {
                    return false;
                }

                await msg.Channel.TriggerTypingAsync().ConfigureAwait(false);

                var response = await cleverbot.Think(message).ConfigureAwait(false);
                try
                {
                    await msg.Channel.SendMessageAsync(response).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    _log.Warn(ex, "Eror sending response");
                    await msg.Channel.SendMessageAsync(response).ConfigureAwait(false); // try twice :\
                }
                return true;
            }

            [NadekoCommand, Usage, Description, Aliases]
            [RequireContext(ContextType.Guild)]
            [RequirePermission(ChannelPermission.ManageMessages)]
            public async Task Cleverbot(IUserMessage imsg)<commit_msg>Cleverbot now mentions person it is talking to<commit_after>﻿using Discord;
using Discord.Commands;
using NadekoBot.Attributes;
using NadekoBot.Services;
using NadekoBot.Services.Database;
using Newtonsoft.Json;
using NLog;
using Services.CleverBotApi;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace NadekoBot.Modules.Games
{
    public partial class Games
    {
        [Group]
        public class CleverBotCommands
        {
            private static Logger _log { get; }

            class CleverAnswer {
                public string Status { get; set; }
                public string Response { get; set; }
            }
            //user#discrim is the key
            public static ConcurrentHashSet<string> ChannelsInConversation { get; } = new ConcurrentHashSet<string>();
            public static ConcurrentDictionary<ulong, ChatterBotSession> CleverbotGuilds { get; } = new ConcurrentDictionary<ulong, ChatterBotSession>();

            static CleverBotCommands()
            {
                _log = LogManager.GetCurrentClassLogger();

                using (var uow = DbHandler.UnitOfWork())
                {
                    var bot = ChatterBotFactory.Create(ChatterBotType.CLEVERBOT);
                    CleverbotGuilds = new ConcurrentDictionary<ulong, ChatterBotSession>(
                        uow.GuildConfigs.GetAll()
                            .Where(gc => gc.CleverbotEnabled)
                            .ToDictionary(gc => gc.GuildId, gc => bot.CreateSession()));
                }
            }

            public static async Task<bool> TryAsk(IUserMessage msg) {
                var channel = msg.Channel as ITextChannel;

                if (channel == null)
                    return false;

                ChatterBotSession cleverbot;
                if (!CleverbotGuilds.TryGetValue(channel.Guild.Id, out cleverbot))
                    return false;

                var nadekoId = NadekoBot.Client.GetCurrentUser().Id;
                var normalMention = $"<@{nadekoId}> ";
                var nickMention = $"<@!{nadekoId}> ";
                string message;
                if (msg.Content.StartsWith(normalMention))
                {
                    message = msg.Content.Substring(normalMention.Length).Trim();
                }
                else if (msg.Content.StartsWith(nickMention))
                {
                    message = msg.Content.Substring(nickMention.Length).Trim();
                }
                else
                {
                    return false;
                }

                await msg.Channel.TriggerTypingAsync().ConfigureAwait(false);

                var response = await cleverbot.Think(message).ConfigureAwait(false);
                try
                {
                    await msg.Channel.SendMessageAsync(response).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    _log.Warn(ex, "Eror sending response");
                    await msg.Channel.SendMessageAsync(msg.Author.Mention+" "+response).ConfigureAwait(false); // try twice :\
                }
                return true;
            }

            [NadekoCommand, Usage, Description, Aliases]
            [RequireContext(ContextType.Guild)]
            [RequirePermission(ChannelPermission.ManageMessages)]
            public async Task Cleverbot(IUserMessage imsg)
{'fraction_non_alphanumeric': 0.08505812305075135, 'fraction_numerical': 0.0, 'mean_word_length': 1.6693151721528565, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6984538', 'n_tokens_mistral': 1858, 'n_tokens_neox': 1752, 'n_words': 369}