Add (broken) Context handler
This commit is contained in:
parent
a65f9d2963
commit
70d92706e2
5 changed files with 149 additions and 6 deletions
97
Geekbot.net/Commands/ContextHandler.cs
Normal file
97
Geekbot.net/Commands/ContextHandler.cs
Normal file
|
@ -0,0 +1,97 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Dynamic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Discord;
|
||||||
|
using Discord.Commands;
|
||||||
|
|
||||||
|
namespace Geekbot.net.Commands
|
||||||
|
{
|
||||||
|
public class ContextHandler : IContextHandler
|
||||||
|
{
|
||||||
|
private Dictionary<ulong, ContextStore> _store;
|
||||||
|
private readonly IDiscordClient _client;
|
||||||
|
private readonly CommandService _commandService;
|
||||||
|
private readonly IServiceProvider _servicesProvider;
|
||||||
|
|
||||||
|
public ContextHandler(IDiscordClient client, CommandService commandService, IServiceProvider servicesProvider)
|
||||||
|
{
|
||||||
|
_store = new Dictionary<ulong, ContextStore>();
|
||||||
|
_commandService = commandService;
|
||||||
|
_servicesProvider = servicesProvider;
|
||||||
|
_client = client;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ContextReference HasContext(IUserMessage message)
|
||||||
|
{
|
||||||
|
if (_store.ContainsKey(message.Author.Id)) return ContextReference.User;
|
||||||
|
|
||||||
|
if (_store.ContainsKey(message.Channel.Id)) return ContextReference.Channel;
|
||||||
|
|
||||||
|
return ContextReference.None;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SaveContext(ContextReference type, ICommandContext context, string commandName)
|
||||||
|
{
|
||||||
|
var contextStore = new ContextStore()
|
||||||
|
{
|
||||||
|
CommandName = commandName
|
||||||
|
};
|
||||||
|
var id = GetId(type, context.Message);
|
||||||
|
_store.Add(id, contextStore);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async void ExecuteOnContext(ContextReference type, IUserMessage message)
|
||||||
|
{
|
||||||
|
var id = GetId(type, message);
|
||||||
|
var obj = _store[id];
|
||||||
|
var context = new CommandContext(_client, message);
|
||||||
|
var rest = await _commandService.ExecuteAsync(context, $"{obj.CommandSearch} {context.Message?.Content}", _servicesProvider);
|
||||||
|
if (!rest.IsSuccess)
|
||||||
|
{
|
||||||
|
await context.Channel.SendMessageAsync(rest.ErrorReason);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ClearContext(ulong id)
|
||||||
|
{
|
||||||
|
_store.Remove(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ulong GetId(ContextReference type, IUserMessage message)
|
||||||
|
{
|
||||||
|
if (type == ContextReference.Channel)
|
||||||
|
{
|
||||||
|
return message.Author.Id;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type == ContextReference.User)
|
||||||
|
{
|
||||||
|
return message.Author.Id;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Exception("No Context Object Found");
|
||||||
|
}
|
||||||
|
|
||||||
|
private class ContextStore
|
||||||
|
{
|
||||||
|
public string CommandName { get; set; }
|
||||||
|
public string CommandSearch => $"{CommandName} ctx";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum ContextReference
|
||||||
|
{
|
||||||
|
User,
|
||||||
|
Channel,
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IContextHandler
|
||||||
|
{
|
||||||
|
ContextReference HasContext(IUserMessage message);
|
||||||
|
void SaveContext(ContextReference type, ICommandContext context, string commandName);
|
||||||
|
void ExecuteOnContext(ContextReference type, IUserMessage message);
|
||||||
|
void ClearContext(ulong id);
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,17 +6,48 @@ using Geekbot.net.Lib;
|
||||||
|
|
||||||
namespace Geekbot.net.Commands
|
namespace Geekbot.net.Commands
|
||||||
{
|
{
|
||||||
|
[Group("say")]
|
||||||
public class Say : ModuleBase
|
public class Say : ModuleBase
|
||||||
{
|
{
|
||||||
private readonly IErrorHandler _errorHandler;
|
private readonly IErrorHandler _errorHandler;
|
||||||
|
private readonly IContextHandler _contextHandler;
|
||||||
|
|
||||||
public Say(IErrorHandler errorHandler)
|
public Say(IErrorHandler errorHandler, IContextHandler contextHandler)
|
||||||
{
|
{
|
||||||
_errorHandler = errorHandler;
|
_errorHandler = errorHandler;
|
||||||
|
_contextHandler = contextHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
[RequireUserPermission(GuildPermission.Administrator)]
|
[RequireUserPermission(GuildPermission.Administrator)]
|
||||||
[Command("say", RunMode = RunMode.Async)]
|
[Command("record", RunMode = RunMode.Async)]
|
||||||
|
[Remarks(CommandCategories.Admin)]
|
||||||
|
[Summary("Say Something.")]
|
||||||
|
public async Task recordEcho()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_contextHandler.SaveContext(ContextReference.User, Context, "say");
|
||||||
|
await ReplyAsync("Recording...");
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_errorHandler.HandleCommandException(e, Context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[RequireUserPermission(GuildPermission.Administrator)]
|
||||||
|
[Command("ctx", RunMode = RunMode.Async)]
|
||||||
|
[Remarks(CommandCategories.Admin)]
|
||||||
|
[Summary("Say Something.")]
|
||||||
|
public async Task recordEcho([Remainder] [Summary("What?")] string echo)
|
||||||
|
{
|
||||||
|
Console.WriteLine("actually got here...");
|
||||||
|
_contextHandler.ClearContext(Context.User.Id);
|
||||||
|
await Context.Channel.SendMessageAsync(echo);
|
||||||
|
}
|
||||||
|
|
||||||
|
[RequireUserPermission(GuildPermission.Administrator)]
|
||||||
|
[Command("ss", RunMode = RunMode.Async)]
|
||||||
[Remarks(CommandCategories.Admin)]
|
[Remarks(CommandCategories.Admin)]
|
||||||
[Summary("Say Something.")]
|
[Summary("Say Something.")]
|
||||||
public async Task Echo([Remainder] [Summary("What?")] string echo)
|
public async Task Echo([Remainder] [Summary("What?")] string echo)
|
||||||
|
|
|
@ -5,6 +5,7 @@ using System.Threading.Tasks;
|
||||||
using Discord;
|
using Discord;
|
||||||
using Discord.Commands;
|
using Discord.Commands;
|
||||||
using Discord.WebSocket;
|
using Discord.WebSocket;
|
||||||
|
using Geekbot.net.Commands;
|
||||||
using Geekbot.net.Lib;
|
using Geekbot.net.Lib;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
using StackExchange.Redis;
|
using StackExchange.Redis;
|
||||||
|
@ -19,8 +20,9 @@ namespace Geekbot.net
|
||||||
private readonly IServiceProvider _servicesProvider;
|
private readonly IServiceProvider _servicesProvider;
|
||||||
private readonly CommandService _commands;
|
private readonly CommandService _commands;
|
||||||
private readonly IUserRepository _userRepository;
|
private readonly IUserRepository _userRepository;
|
||||||
|
private readonly IContextHandler _contextHandler;
|
||||||
|
|
||||||
public Handlers(IDiscordClient client, IGeekbotLogger logger, IDatabase redis, IServiceProvider servicesProvider, CommandService commands, IUserRepository userRepository)
|
public Handlers(IDiscordClient client, IGeekbotLogger logger, IDatabase redis, IServiceProvider servicesProvider, CommandService commands, IUserRepository userRepository, IContextHandler contextHandler)
|
||||||
{
|
{
|
||||||
_client = client;
|
_client = client;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
|
@ -28,6 +30,7 @@ namespace Geekbot.net
|
||||||
_servicesProvider = servicesProvider;
|
_servicesProvider = servicesProvider;
|
||||||
_commands = commands;
|
_commands = commands;
|
||||||
_userRepository = userRepository;
|
_userRepository = userRepository;
|
||||||
|
_contextHandler = contextHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -52,6 +55,13 @@ namespace Geekbot.net
|
||||||
message.Channel.SendMessageAsync("hui!!!");
|
message.Channel.SendMessageAsync("hui!!!");
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var contextType = _contextHandler.HasContext(message);
|
||||||
|
if (contextType != ContextReference.None)
|
||||||
|
{
|
||||||
|
_contextHandler.ExecuteOnContext(contextType, message);
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
if (!(message.HasCharPrefix('!', ref argPos) ||
|
if (!(message.HasCharPrefix('!', ref argPos) ||
|
||||||
message.HasMentionPrefix(_client.CurrentUser, ref argPos))) return Task.CompletedTask;
|
message.HasMentionPrefix(_client.CurrentUser, ref argPos))) return Task.CompletedTask;
|
||||||
var context = new CommandContext(_client, message);
|
var context = new CommandContext(_client, message);
|
||||||
|
|
|
@ -48,6 +48,8 @@ namespace Geekbot.net.Lib
|
||||||
Context.Channel.SendMessageAsync(errorString);
|
Context.Channel.SendMessageAsync(errorString);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (e.Message.Contains("50013")) return;
|
||||||
|
if (e.Message.Contains("50007")) return;
|
||||||
if (_raven == null) return;
|
if (_raven == null) return;
|
||||||
|
|
||||||
var sentryEvent = new SentryEvent(e)
|
var sentryEvent = new SentryEvent(e)
|
||||||
|
|
|
@ -10,6 +10,7 @@ using System.Threading.Tasks;
|
||||||
using Discord;
|
using Discord;
|
||||||
using Discord.Commands;
|
using Discord.Commands;
|
||||||
using Discord.WebSocket;
|
using Discord.WebSocket;
|
||||||
|
using Geekbot.net.Commands;
|
||||||
using Geekbot.net.Lib;
|
using Geekbot.net.Lib;
|
||||||
using Geekbot.net.Lib.Media;
|
using Geekbot.net.Lib.Media;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
@ -136,13 +137,15 @@ namespace Geekbot.net
|
||||||
var translationHandler = new TranslationHandler(client.Guilds, redis, logger);
|
var translationHandler = new TranslationHandler(client.Guilds, redis, logger);
|
||||||
var errorHandler = new ErrorHandler(logger, translationHandler);
|
var errorHandler = new ErrorHandler(logger, translationHandler);
|
||||||
await commands.AddModulesAsync(Assembly.GetEntryAssembly());
|
await commands.AddModulesAsync(Assembly.GetEntryAssembly());
|
||||||
|
var contextHandler = new ContextHandler(client, commands, servicesProvider);
|
||||||
services.AddSingleton(commands);
|
services.AddSingleton(commands);
|
||||||
|
services.AddSingleton<IContextHandler>(contextHandler);
|
||||||
services.AddSingleton<IErrorHandler>(errorHandler);
|
services.AddSingleton<IErrorHandler>(errorHandler);
|
||||||
services.AddSingleton<ITranslationHandler>(translationHandler);
|
services.AddSingleton<ITranslationHandler>(translationHandler);
|
||||||
services.AddSingleton<DiscordSocketClient>(client);
|
services.AddSingleton<DiscordSocketClient>(client);
|
||||||
servicesProvider = services.BuildServiceProvider();
|
servicesProvider = services.BuildServiceProvider();
|
||||||
|
|
||||||
var handlers = new Handlers(client, logger, redis, servicesProvider, commands, userRepository);
|
var handlers = new Handlers(client, logger, redis, servicesProvider, commands, userRepository, contextHandler);
|
||||||
|
|
||||||
client.MessageReceived += handlers.RunCommand;
|
client.MessageReceived += handlers.RunCommand;
|
||||||
client.MessageReceived += handlers.UpdateStats;
|
client.MessageReceived += handlers.UpdateStats;
|
||||||
|
|
Loading…
Reference in a new issue