disabling >cleverbot with permissions will also disable talking with chatterbot
This commit is contained in:
parent
3e1b5d7e57
commit
bbda4c7a3b
@ -40,16 +40,19 @@ namespace NadekoBot.Modules.Games
|
|||||||
_log.Debug($"Loaded in {sw.Elapsed.TotalSeconds:F2}s");
|
_log.Debug($"Loaded in {sw.Elapsed.TotalSeconds:F2}s");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task<bool> TryAsk(IUserMessage msg)
|
public static string PrepareMessage(IUserMessage msg, out ChatterBotSession cleverbot)
|
||||||
{
|
{
|
||||||
var channel = msg.Channel as ITextChannel;
|
var channel = msg.Channel as ITextChannel;
|
||||||
|
cleverbot = null;
|
||||||
|
|
||||||
if (channel == null)
|
if (channel == null)
|
||||||
return false;
|
return null;
|
||||||
|
|
||||||
Lazy<ChatterBotSession> cleverbot;
|
Lazy<ChatterBotSession> lazyCleverbot;
|
||||||
if (!CleverbotGuilds.TryGetValue(channel.Guild.Id, out cleverbot))
|
if (!CleverbotGuilds.TryGetValue(channel.Guild.Id, out lazyCleverbot))
|
||||||
return false;
|
return null;
|
||||||
|
|
||||||
|
cleverbot = lazyCleverbot.Value;
|
||||||
|
|
||||||
var nadekoId = NadekoBot.Client.CurrentUser.Id;
|
var nadekoId = NadekoBot.Client.CurrentUser.Id;
|
||||||
var normalMention = $"<@{nadekoId}> ";
|
var normalMention = $"<@{nadekoId}> ";
|
||||||
@ -65,19 +68,24 @@ namespace NadekoBot.Modules.Games
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
await msg.Channel.TriggerTypingAsync().ConfigureAwait(false);
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
var response = await cleverbot.Value.Think(message).ConfigureAwait(false);
|
public static async Task<bool> TryAsk(ChatterBotSession cleverbot, ITextChannel channel, string message)
|
||||||
|
{
|
||||||
|
await channel.TriggerTypingAsync().ConfigureAwait(false);
|
||||||
|
|
||||||
|
var response = await cleverbot.Think(message).ConfigureAwait(false);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await msg.Channel.SendConfirmAsync(response.SanitizeMentions()).ConfigureAwait(false);
|
await channel.SendConfirmAsync(response.SanitizeMentions()).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
await msg.Channel.SendConfirmAsync(response.SanitizeMentions()).ConfigureAwait(false); // try twice :\
|
await channel.SendConfirmAsync(response.SanitizeMentions()).ConfigureAwait(false); // try twice :\
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -62,6 +62,24 @@ namespace NadekoBot.Modules.Permissions
|
|||||||
log.Debug($"Loaded in {sw.Elapsed.TotalSeconds:F2}s");
|
log.Debug($"Loaded in {sw.Elapsed.TotalSeconds:F2}s");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static PermissionCache GetCache(ulong guildId)
|
||||||
|
{
|
||||||
|
PermissionCache pc;
|
||||||
|
if (!Permissions.Cache.TryGetValue(guildId, out pc))
|
||||||
|
{
|
||||||
|
using (var uow = DbHandler.UnitOfWork())
|
||||||
|
{
|
||||||
|
var config = uow.GuildConfigs.For(guildId,
|
||||||
|
set => set.Include(x => x.Permissions));
|
||||||
|
Permissions.UpdateCache(config);
|
||||||
|
}
|
||||||
|
Permissions.Cache.TryGetValue(guildId, out pc);
|
||||||
|
if (pc == null)
|
||||||
|
throw new Exception("Cache is null.");
|
||||||
|
}
|
||||||
|
return pc;
|
||||||
|
}
|
||||||
|
|
||||||
private static void TryMigratePermissions()
|
private static void TryMigratePermissions()
|
||||||
{
|
{
|
||||||
var log = LogManager.GetCurrentClassLogger();
|
var log = LogManager.GetCurrentClassLogger();
|
||||||
|
@ -18,6 +18,7 @@ using System.Collections.Concurrent;
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using NadekoBot.DataStructures;
|
using NadekoBot.DataStructures;
|
||||||
|
using Services.CleverBotApi;
|
||||||
|
|
||||||
namespace NadekoBot.Services
|
namespace NadekoBot.Services
|
||||||
{
|
{
|
||||||
@ -109,13 +110,33 @@ namespace NadekoBot.Services
|
|||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<bool> TryRunCleverbot(IUserMessage usrMsg, IGuild guild)
|
private async Task<bool> TryRunCleverbot(IUserMessage usrMsg, SocketGuild guild)
|
||||||
{
|
{
|
||||||
if (guild == null)
|
if (guild == null)
|
||||||
return false;
|
return false;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var cleverbotExecuted = await Games.CleverBotCommands.TryAsk(usrMsg).ConfigureAwait(false);
|
Games.ChatterBotSession cbs;
|
||||||
|
var message = Games.CleverBotCommands.PrepareMessage(usrMsg, out cbs);
|
||||||
|
if(message == null || cbs == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
PermissionCache pc = Permissions.GetCache(guild.Id);
|
||||||
|
int index;
|
||||||
|
if (
|
||||||
|
!pc.Permissions.CheckPermissions(usrMsg,
|
||||||
|
NadekoBot.ModulePrefixes[typeof(Games).Name] + "cleverbot",
|
||||||
|
typeof(Games).Name,
|
||||||
|
out index))
|
||||||
|
{
|
||||||
|
//todo print in guild actually
|
||||||
|
var returnMsg =
|
||||||
|
$"Permission number #{index + 1} **{pc.Permissions[index].GetCommand(guild)}** is preventing this action.";
|
||||||
|
_log.Info(returnMsg);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
var cleverbotExecuted = await Games.CleverBotCommands.TryAsk(cbs, (ITextChannel)usrMsg.Channel, message).ConfigureAwait(false);
|
||||||
if (cleverbotExecuted)
|
if (cleverbotExecuted)
|
||||||
{
|
{
|
||||||
_log.Info($@"CleverBot Executed
|
_log.Info($@"CleverBot Executed
|
||||||
@ -278,6 +299,7 @@ namespace NadekoBot.Services
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
var exec1 = Environment.TickCount - execTime;
|
var exec1 = Environment.TickCount - execTime;
|
||||||
|
|
||||||
|
|
||||||
var cleverBotRan = await Task.Run(() => TryRunCleverbot(usrMsg, guild)).ConfigureAwait(false);
|
var cleverBotRan = await Task.Run(() => TryRunCleverbot(usrMsg, guild)).ConfigureAwait(false);
|
||||||
if (cleverBotRan)
|
if (cleverBotRan)
|
||||||
@ -294,19 +316,8 @@ namespace NadekoBot.Services
|
|||||||
{
|
{
|
||||||
if (guild != null)
|
if (guild != null)
|
||||||
{
|
{
|
||||||
PermissionCache pc;
|
PermissionCache pc = Permissions.GetCache(guild.Id);
|
||||||
if (!Permissions.Cache.TryGetValue(guild.Id, out pc))
|
|
||||||
{
|
|
||||||
using (var uow = DbHandler.UnitOfWork())
|
|
||||||
{
|
|
||||||
var config = uow.GuildConfigs.For(guild.Id,
|
|
||||||
set => set.Include(x => x.Permissions));
|
|
||||||
Permissions.UpdateCache(config);
|
|
||||||
}
|
|
||||||
Permissions.Cache.TryGetValue(guild.Id, out pc);
|
|
||||||
if (pc == null)
|
|
||||||
throw new Exception("Cache is null.");
|
|
||||||
}
|
|
||||||
int index;
|
int index;
|
||||||
if (
|
if (
|
||||||
!pc.Permissions.CheckPermissions(usrMsg, cr.Trigger, "ActualCustomReactions",
|
!pc.Permissions.CheckPermissions(usrMsg, cr.Trigger, "ActualCustomReactions",
|
||||||
@ -457,21 +468,9 @@ namespace NadekoBot.Services
|
|||||||
var cmd = commands[i].Command;
|
var cmd = commands[i].Command;
|
||||||
var resetCommand = cmd.Name == "resetperms";
|
var resetCommand = cmd.Name == "resetperms";
|
||||||
var module = cmd.Module.GetTopLevelModule();
|
var module = cmd.Module.GetTopLevelModule();
|
||||||
PermissionCache pc;
|
|
||||||
if (context.Guild != null)
|
if (context.Guild != null)
|
||||||
{
|
{
|
||||||
//todo move to permissions module?
|
PermissionCache pc = Permissions.GetCache(context.Guild.Id);
|
||||||
if (!Permissions.Cache.TryGetValue(context.Guild.Id, out pc))
|
|
||||||
{
|
|
||||||
using (var uow = DbHandler.UnitOfWork())
|
|
||||||
{
|
|
||||||
var config = uow.GuildConfigs.GcWithPermissionsv2For(context.Guild.Id);
|
|
||||||
Permissions.UpdateCache(config);
|
|
||||||
}
|
|
||||||
Permissions.Cache.TryGetValue(context.Guild.Id, out pc);
|
|
||||||
if(pc == null)
|
|
||||||
throw new Exception("Cache is null.");
|
|
||||||
}
|
|
||||||
int index;
|
int index;
|
||||||
if (!resetCommand && !pc.Permissions.CheckPermissions(context.Message, cmd.Aliases.First(), module.Name, out index))
|
if (!resetCommand && !pc.Permissions.CheckPermissions(context.Message, cmd.Aliases.First(), module.Name, out index))
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user