Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions DiscordSharp.Commands/CommandsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DiscordSharp.Discord;

namespace DiscordSharp.Commands
{
public class ModuleNotEnabledException : Exception
{
IModule module;
private IModule module;

public IModule Module
{
get { return module; }
}

public ModuleNotEnabledException(string message, IModule module) : base(message)
{
this.module = module;
Expand All @@ -22,12 +25,15 @@ public ModuleNotEnabledException(string message, IModule module) : base(message)

public class BaseModuleToggleException : Exception
{
public BaseModuleToggleException(string message) : base(message) { }
public BaseModuleToggleException(string message) : base(message)
{
}
}

public class CommandsManager
{
private readonly DiscordClient __client;

public DiscordClient Client
{
get
Expand All @@ -39,6 +45,7 @@ public DiscordClient Client
public Random rng = new Random((int)DateTime.Now.Ticks);

private List<ICommand> __commands;

public List<ICommand> Commands
{
get { return __commands; }
Expand All @@ -50,13 +57,15 @@ public List<ICommand> Commands
/// Value = Whether or not the module is enabled.
/// </summary>
private Dictionary<IModule, bool> __modules;

public Dictionary<IModule, bool> Modules
{
get { return __modules; }
}

//id, permission
//id, permission
private static Dictionary<string, PermissionType> __internalUserRoles;

public static Dictionary<string, PermissionType> UserRoles
{
get
Expand All @@ -69,7 +78,7 @@ internal static PermissionType GetPermissionFromID(string id)
{
if (__internalUserRoles.Count > 0)
{
foreach(var perm in __internalUserRoles)
foreach (var perm in __internalUserRoles)
{
if (perm.Key == id)
return perm.Value;
Expand All @@ -91,7 +100,7 @@ public CommandsManager(DiscordClient client)

public bool HasPermission(DiscordMember member, PermissionType permission)
{
if(__internalUserRoles.ContainsKey(member.ID))
if (__internalUserRoles.ContainsKey(member.ID))
{
foreach (var perm in __internalUserRoles)
if (perm.Key == member.ID && (int)perm.Value >= (int)permission)
Expand All @@ -106,6 +115,7 @@ public void AddPermission(DiscordMember member, PermissionType permission)
__internalUserRoles.Remove(member.ID);
__internalUserRoles.Add(member.ID, permission);
}

public void AddPermission(string memberID, PermissionType permission)
{
if (__internalUserRoles.ContainsKey(memberID))
Expand All @@ -131,7 +141,7 @@ public Dictionary<string, bool> ModuleDictionaryForJson()
{
Dictionary<string, bool> dict = new Dictionary<string, bool>();

lock(__modules)
lock (__modules)
{
foreach (var kvp in __modules)
{
Expand All @@ -152,7 +162,7 @@ public int ExecuteOnMessageCommand(string rawCommandText, DiscordChannel channel

if (command != null && command.Parent != null) //if it's a generic command without a parent then don't bother doing this.
{
lock(__modules)
lock (__modules)
{
if (__modules[command.Parent] == false)
{
Expand All @@ -161,7 +171,7 @@ public int ExecuteOnMessageCommand(string rawCommandText, DiscordChannel channel
}
}

if(command != null)
if (command != null)
{
command.Args.Clear();
if (command.ArgCount > 0)
Expand All @@ -176,11 +186,11 @@ public int ExecuteOnMessageCommand(string rawCommandText, DiscordChannel channel
return 0;
}
}
catch(UnauthorizedAccessException uaex)
catch (UnauthorizedAccessException uaex)
{
throw uaex; //no permission
}
catch(Exception ex)
catch (Exception ex)
{
throw ex;
}
Expand All @@ -189,7 +199,7 @@ public int ExecuteOnMessageCommand(string rawCommandText, DiscordChannel channel

public bool ModuleEnabled(string name)
{
lock(__modules)
lock (__modules)
{
foreach (var kvp in __modules)
{
Expand All @@ -204,7 +214,7 @@ public bool ModuleEnabled(string name)

public void EnableModule(string name)
{
lock(__modules)
lock (__modules)
{
foreach (var kvp in __modules)
{
Expand All @@ -222,7 +232,7 @@ public void DisableModule(string name)
if (name.ToLower().Trim() == "base")
throw new BaseModuleToggleException("Can't disable base module!");

lock(__modules)
lock (__modules)
{
foreach (var kvp in __modules)
{
Expand Down Expand Up @@ -250,7 +260,7 @@ public void AddCommand(ICommand command, IModule fromModule)
{
command.Parent = fromModule;
command.Parent.Commands.Add(command);
lock(__modules)
lock (__modules)
{
if (!__modules.ContainsKey(fromModule))
__modules.Add(fromModule, true);
Expand All @@ -262,4 +272,4 @@ public void AddCommand(ICommand command, IModule fromModule)
__commands.Add(command);
}
}
}
}
Loading