You can now add CleverBotApiKey from cleverbot.com/api in order to use official cleverbot, instead of stupid program-o

This commit is contained in:
Master Kwoth 2017-07-20 22:58:19 +02:00
parent cedaf73785
commit 613655eb95
12 changed files with 109 additions and 28 deletions

View File

@ -401,6 +401,7 @@ Commands and aliases | Description | Usage
`.listservers` | Lists servers the bot is on with some basic info. 15 per page. **Bot owner only** | `.listservers 3`
`.savechat` | Saves a number of messages to a text file and sends it to you. **Bot owner only** | `.savechat 150`
`.ping` | Ping the bot to see if there are latency issues. | `.ping`
`.botconfigedit` `.bce` | Sets one of available bot config settings to a specified value. Use the command without any parameters to get a list of available settings. **Bot owner only** | `.bce CurrencyName b1nzy` or `.bce`
`.calculate` `.calc` | Evaluate a mathematical expression. | `.calc 1+1`
`.calcops` | Shows all available operations in the `.calc` command | `.calcops`
`.alias` `.cmdmap` | Create a custom alias for a certain Nadeko command. Provide no alias to remove the existing one. **Requires Administrator server permission.** | `.alias allin $bf 100 h` or `.alias "linux thingy" >loonix Spyware Windows`
@ -425,6 +426,9 @@ Commands and aliases | Description | Usage
`.repeat` | Repeat a message every `X` minutes in the current channel. You can instead specify time of day for the message to be repeated at daily (make sure you've set your server's timezone). You can have up to 5 repeating messages on the server in total. **Requires ManageMessages server permission.** | `.repeat 5 Hello there` or `.repeat 17:30 tea time`
`.repeatlist` `.replst` | Shows currently repeating messages and their indexes. **Requires ManageMessages server permission.** | `.repeatlist`
`.streamrole` | Sets a role which is monitored for streamers (FromRole), and a role to add if a user from 'FromRole' is streaming (AddRole). When a user from 'FromRole' starts streaming, they will receive an 'AddRole'. Provide no arguments to disable **Requires ManageRoles server permission.** | `.streamrole "Eligible Streamers" "Featured Streams"`
`.streamrolekw` `.srkw` | Sets keyword which is required in the stream's title in order for the streamrole to apply. Provide no keyword in order to reset. **Requires ManageRoles server permission.** | `.srkw` or `.srkw PUBG`
`.streamrolebl` `.srbl` | Adds or removes a blacklisted user. Blacklisted users will never receive the stream role. **Requires ManageRoles server permission.** | `.srbl add @b1nzy#1234` or `.srbl rem @b1nzy#1234`
`.streamrolewl` `.srwl` | Adds or removes a whitelisted user. Whitelisted users will receive the stream role even if they don't have the specified keyword in their stream title. **Requires ManageRoles server permission.** | `.srwl add @b1nzy#1234` or `.srwl rem @b1nzy#1234`
`.convertlist` | List of the convertible dimensions and currencies. | `.convertlist`
`.convert` | Convert quantities. Use `.convertlist` to see supported dimensions and currencies. | `.convert m km 1000`
`.verboseerror` `.ve` | Toggles whether the bot should print command errors when a command is incorrectly used. **Requires ManageMessages server permission.** | `.ve`

View File

@ -16,6 +16,7 @@ If you do not see `credentials.json` you will need to rename `credentials_exampl
"GoogleApiKey": "AIzaSyDSci1sdlWQOWNVj1vlXxxxxxbk0oWMEzM",
"MashapeKey": "4UrKpcWXc2mshS8RKi00000y8Kf5p1Q8kI6jsn32bmd8oVWiY7",
"OsuApiKey": "4c8c8fdff8e1234581725db27fd140a7d93320d6",
"CleverbotApiKey": "",
"PatreonAccessToken": "",
"PatreonCampaignId": "334038",
"Db": null,
@ -148,12 +149,15 @@ It should look like:
- Required for Urban Disctionary, Hashtag search, and Hearthstone cards.
- You need to create an account on their [api marketplace](https://market.mashape.com/), after that go to `market.mashape.com/YOURNAMEHERE/applications/default-application` and press **Get the keys** in the top right corner.
- Copy the key and paste it into `credentials.json`
- **LOLAPIKey**
- **LoLApiKey**
- Required for all League of Legends commands.
- You can get this key [here](http://api.champion.gg/)
- **OsuAPIKey**
- You can get this key [here.](http://api.champion.gg/)
- **OsuApiKey**
- Required for Osu commands
- You can get this key [here.](https://osu.ppy.sh/p/api)
- You can get this key [here.](https://osu.ppy.sh/p/api)
- **CleverbotApiKey**
- Required if you want to use official cleverobot, instead of program-o
- you can get this key [here.](http://www.cleverbot.com/api/)
- **PatreonAccessToken**
- For Patreon creators only.
- **PatreonCampaignId**

View File

@ -4,19 +4,19 @@ using NadekoBot.Services;
using System;
using System.Threading.Tasks;
using NadekoBot.Common.Attributes;
using NadekoBot.Modules.Games.Common;
using NadekoBot.Modules.Games.Services;
using NadekoBot.Modules.Games.Common.ChatterBot;
namespace NadekoBot.Modules.Games
{
public partial class Games
{
[Group]
public class CleverBotCommands : NadekoSubmodule<ChatterBotService>
public class ChatterBotCommands : NadekoSubmodule<ChatterBotService>
{
private readonly DbService _db;
public CleverBotCommands(DbService db)
public ChatterBotCommands(DbService db)
{
_db = db;
}
@ -28,7 +28,7 @@ namespace NadekoBot.Modules.Games
{
var channel = (ITextChannel)Context.Channel;
if (_service.ChatterBotGuilds.TryRemove(channel.Guild.Id, out Lazy<ChatterBotSession> throwaway))
if (_service.ChatterBotGuilds.TryRemove(channel.Guild.Id, out Lazy<IChatterBotSession> throwaway))
{
using (var uow = _db.UnitOfWork)
{
@ -39,7 +39,7 @@ namespace NadekoBot.Modules.Games
return;
}
_service.ChatterBotGuilds.TryAdd(channel.Guild.Id, new Lazy<ChatterBotSession>(() => new ChatterBotSession(Context.Guild.Id), true));
_service.ChatterBotGuilds.TryAdd(channel.Guild.Id, new Lazy<IChatterBotSession>(() => _service.CreateSession(), true));
using (var uow = _db.UnitOfWork)
{

View File

@ -1,4 +1,4 @@
namespace NadekoBot.Modules.Games.Common
namespace NadekoBot.Modules.Games.Common.ChatterBot
{
public class ChatterBotResponse
{

View File

@ -4,25 +4,24 @@ using NadekoBot.Common;
using NadekoBot.Extensions;
using Newtonsoft.Json;
namespace NadekoBot.Modules.Games.Common
namespace NadekoBot.Modules.Games.Common.ChatterBot
{
public class ChatterBotSession
public class ChatterBotSession : IChatterBotSession
{
private static NadekoRandom rng { get; } = new NadekoRandom();
public string ChatterbotId { get; }
public string ChannelId { get; }
private static NadekoRandom Rng { get; } = new NadekoRandom();
private readonly string _chatterBotId;
private int _botId = 6;
public ChatterBotSession(ulong channelId)
public ChatterBotSession()
{
ChannelId = channelId.ToString().ToBase64();
ChatterbotId = rng.Next(0, 1000000).ToString().ToBase64();
_chatterBotId = Rng.Next(0, 1000000).ToString().ToBase64();
}
private string apiEndpoint => "http://api.program-o.com/v2/chatbot/" +
$"?bot_id={_botId}&" +
"say={0}&" +
$"convo_id=nadekobot_{ChatterbotId}_{ChannelId}&" +
$"convo_id=nadekobot_{_chatterBotId}&" +
"format=json";
public async Task<string> Think(string message)

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NadekoBot.Modules.Games.Common.ChatterBot
{
public class CleverbotResponse
{
public string Cs { get; set; }
public string Output { get; set; }
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NadekoBot.Modules.Games.Common.ChatterBot
{
public interface IChatterBotSession
{
Task<string> Think(string input);
}
}

View File

@ -0,0 +1,33 @@
using Newtonsoft.Json;
using System.Net.Http;
using System.Threading.Tasks;
namespace NadekoBot.Modules.Games.Common.ChatterBot
{
public class OfficialCleverbotSession : IChatterBotSession
{
private readonly string _apiKey;
private string _cs = null;
private string queryString => $"https://www.cleverbot.com/getreply?key={_apiKey}" +
"&wrapper=nadekobot" +
"&input={0}" +
"&cs={1}";
public OfficialCleverbotSession(string apiKey)
{
this._apiKey = apiKey;
}
public async Task<string> Think(string input)
{
using (var http = new HttpClient())
{
var dataString = await http.GetStringAsync(string.Format(queryString, input, _cs ?? "")).ConfigureAwait(false);
var data = JsonConvert.DeserializeObject<CleverbotResponse>(dataString);
_cs = data?.Cs;
return data?.Output;
}
}
}
}

View File

@ -7,13 +7,13 @@ using Discord;
using Discord.WebSocket;
using NadekoBot.Common.ModuleBehaviors;
using NadekoBot.Extensions;
using NadekoBot.Modules.Games.Common;
using NadekoBot.Modules.Permissions.Common;
using NadekoBot.Modules.Permissions.Services;
using NadekoBot.Services;
using NadekoBot.Services.Database.Models;
using NadekoBot.Services.Impl;
using NLog;
using NadekoBot.Modules.Games.Common.ChatterBot;
namespace NadekoBot.Modules.Games.Services
{
@ -24,24 +24,34 @@ namespace NadekoBot.Modules.Games.Services
private readonly PermissionService _perms;
private readonly CommandHandler _cmd;
private readonly NadekoStrings _strings;
private readonly IBotCredentials _creds;
public ConcurrentDictionary<ulong, Lazy<ChatterBotSession>> ChatterBotGuilds { get; }
public ConcurrentDictionary<ulong, Lazy<IChatterBotSession>> ChatterBotGuilds { get; }
public ChatterBotService(DiscordSocketClient client, PermissionService perms, IEnumerable<GuildConfig> gcs,
CommandHandler cmd, NadekoStrings strings)
CommandHandler cmd, NadekoStrings strings, IBotCredentials creds)
{
_client = client;
_log = LogManager.GetCurrentClassLogger();
_perms = perms;
_cmd = cmd;
_strings = strings;
_creds = creds;
ChatterBotGuilds = new ConcurrentDictionary<ulong, Lazy<ChatterBotSession>>(
ChatterBotGuilds = new ConcurrentDictionary<ulong, Lazy<IChatterBotSession>>(
gcs.Where(gc => gc.CleverbotEnabled)
.ToDictionary(gc => gc.GuildId, gc => new Lazy<ChatterBotSession>(() => new ChatterBotSession(gc.GuildId), true)));
.ToDictionary(gc => gc.GuildId, gc => new Lazy<IChatterBotSession>(() => CreateSession(), true)));
}
public string PrepareMessage(IUserMessage msg, out ChatterBotSession cleverbot)
public IChatterBotSession CreateSession()
{
if (string.IsNullOrWhiteSpace(_creds.CleverbotApiKey))
return new ChatterBotSession();
else
return new OfficialCleverbotSession(_creds.CleverbotApiKey);
}
public string PrepareMessage(IUserMessage msg, out IChatterBotSession cleverbot)
{
var channel = msg.Channel as ITextChannel;
cleverbot = null;
@ -49,7 +59,7 @@ namespace NadekoBot.Modules.Games.Services
if (channel == null)
return null;
if (!ChatterBotGuilds.TryGetValue(channel.Guild.Id, out Lazy<ChatterBotSession> lazyCleverbot))
if (!ChatterBotGuilds.TryGetValue(channel.Guild.Id, out Lazy<IChatterBotSession> lazyCleverbot))
return null;
cleverbot = lazyCleverbot.Value;
@ -74,7 +84,7 @@ namespace NadekoBot.Modules.Games.Services
return message;
}
public async Task<bool> TryAsk(ChatterBotSession cleverbot, ITextChannel channel, string message)
public async Task<bool> TryAsk(IChatterBotSession cleverbot, ITextChannel channel, string message)
{
await channel.TriggerTypingAsync().ConfigureAwait(false);
@ -96,7 +106,7 @@ namespace NadekoBot.Modules.Games.Services
return false;
try
{
var message = PrepareMessage(usrMsg, out ChatterBotSession cbs);
var message = PrepareMessage(usrMsg, out IChatterBotSession cbs);
if (message == null || cbs == null)
return false;

View File

@ -23,6 +23,7 @@ namespace NadekoBot.Services
string ShardRunCommand { get; }
string ShardRunArguments { get; }
string PatreonCampaignId { get; }
string CleverbotApiKey { get; }
}
public class DBConfig

View File

@ -26,6 +26,7 @@ namespace NadekoBot.Services.Impl
public string LoLApiKey { get; }
public string OsuApiKey { get; }
public string CleverbotApiKey { get; }
public DBConfig Db { get; }
public int TotalShards { get; }
@ -121,6 +122,7 @@ namespace NadekoBot.Services.Impl
public string MashapeKey { get; set; } = "";
public string OsuApiKey { get; set; } = "";
public string SoundCloudClientId { get; set; } = "";
public string CleverbotApiKey { get; } = "";
public string CarbonKey { get; set; } = "";
public DBConfig Db { get; set; } = new DBConfig("sqlite", "Filename=./data/NadekoBot.db");
public int TotalShards { get; set; } = 1;

View File

@ -9,6 +9,7 @@
"MashapeKey": "",
"OsuApiKey": "",
"SoundCloudClientId": "",
"CleverbotApiKey": "",
"CarbonKey": "",
"Db": {
"Type": "sqlite",