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
|
||||
{
|
||||
[Group("say")]
|
||||
public class Say : ModuleBase
|
||||
{
|
||||
private readonly IErrorHandler _errorHandler;
|
||||
|
||||
public Say(IErrorHandler errorHandler)
|
||||
private readonly IContextHandler _contextHandler;
|
||||
|
||||
public Say(IErrorHandler errorHandler, IContextHandler contextHandler)
|
||||
{
|
||||
_errorHandler = errorHandler;
|
||||
_contextHandler = contextHandler;
|
||||
}
|
||||
|
||||
[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)]
|
||||
[Summary("Say Something.")]
|
||||
public async Task Echo([Remainder] [Summary("What?")] string echo)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue