Browse Source

Merge pull request #502 from Joe4evr/Before/AfterExecute

Add BeforeExecute/AfterExecute methods to ModuleBase
tags/1.0-rc
RogueException GitHub 8 years ago
parent
commit
4cf6d8e03f
3 changed files with 18 additions and 0 deletions
  1. +2
    -0
      src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs
  2. +4
    -0
      src/Discord.Net.Commands/IModuleBase.cs
  3. +12
    -0
      src/Discord.Net.Commands/ModuleBase.cs

+ 2
- 0
src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs 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();
} }
}; };


+ 4
- 0
src/Discord.Net.Commands/IModuleBase.cs View File

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

void BeforeExecute();
void AfterExecute();
} }
} }

+ 12
- 0
src/Discord.Net.Commands/ModuleBase.cs 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();
} }
} }

Loading…
Cancel
Save