Merge pull request #502 from Joe4evr/Before/AfterExecute

Add BeforeExecute/AfterExecute methods to ModuleBase
This commit is contained in:
RogueException
2017-01-30 16:39:40 -04:00
committed by GitHub
3 changed files with 18 additions and 0 deletions

View File

@@ -166,10 +166,12 @@ namespace Discord.Commands
instance.SetContext(ctx); instance.SetContext(ctx);
try try
{ {
instance.BeforeExecute();
return method.Invoke(instance, args) as Task ?? Task.Delay(0); return method.Invoke(instance, args) as Task ?? Task.Delay(0);
} }
finally finally
{ {
instance.AfterExecute();
(instance as IDisposable)?.Dispose(); (instance as IDisposable)?.Dispose();
} }
}; };

View File

@@ -3,5 +3,9 @@
internal interface IModuleBase internal interface IModuleBase
{ {
void SetContext(ICommandContext context); void SetContext(ICommandContext context);
void BeforeExecute();
void AfterExecute();
} }
} }

View File

@@ -15,6 +15,14 @@ namespace Discord.Commands
return await Context.Channel.SendMessageAsync(message, isTTS, embed, options).ConfigureAwait(false); return await Context.Channel.SendMessageAsync(message, isTTS, embed, options).ConfigureAwait(false);
} }
protected virtual void BeforeExecute()
{
}
protected virtual void AfterExecute()
{
}
//IModuleBase //IModuleBase
void IModuleBase.SetContext(ICommandContext context) void IModuleBase.SetContext(ICommandContext context)
{ {
@@ -23,5 +31,9 @@ namespace Discord.Commands
throw new InvalidOperationException($"Invalid context type. Expected {typeof(T).Name}, got {context.GetType().Name}"); throw new InvalidOperationException($"Invalid context type. Expected {typeof(T).Name}, got {context.GetType().Name}");
Context = newValue; Context = newValue;
} }
void IModuleBase.BeforeExecute() => BeforeExecute();
void IModuleBase.AfterExecute() => AfterExecute();
} }
} }