public static bool CheckIfOwner(LinkedMessage e) { LinkedServer server = e.server; LinkedUser user = e.author; if (user.isIrc) { LinkedIrcUser ircUser = (LinkedIrcUser)user; if (ircUser.IrcUser.Match(Program.Config.servers[server.name].botOwnerHostmask)) { return(true); } } else if (user.isDiscord) { if (user.id.Equals(Program.Config.DiscordBotOwnerID.ToString())) { return(true); } } return(false); }
public static bool CheckPermission(string commandName, LinkedMessage e) { LinkedServer server = e.server; LinkedChannel channel = e.channel; LinkedUser user = e.author; if (CheckIfOwner(e)) { return(true); // If user is the Bot owner, bypass all permission checks } ICommand command = Program.CommandList[commandName]; // Get required permission level PermissionLevel permission = (PermissionLevel)Attribute.GetCustomAttribute(command.GetType(), typeof(PermissionLevel)); if (permission == null) { Logger.Warn("Command \"{0}\" does not have a valid permission set on the class", commandName); } if (Program.Permissions.TryGetValue(server.name, out Dictionary <string, Dictionary <string, PermissionLevel> > channels)) { string check = channel == null ? "PM" : channel.name; if (channels.TryGetValue(check, out Dictionary <string, PermissionLevel> commands)) { commands.TryGetValue(commandName, out permission); } } // Check permissions if (permission == null) { Logger.Warn("Disallowing use of command \"{0}\" because no permission is set", commandName); return(false); // Prevent accidentally allowing dangerous commands to be ran by normal users } if (permission.minLevel == Modes.BotOwner) { return(false); } if (user.isIrc) { if (permission.minLevel == Modes.None) { return(true); } if (channel == null) { return(false); } LinkedIrcChannel ircChannel = (LinkedIrcChannel)channel; LinkedIrcUser ircUser = (LinkedIrcUser)user; return(ircChannel.channel.UsersByMode[IrcUtils.GetUserLevelChar(permission.minLevel)].Contains(ircUser)); } if (user.isDiscord) { if (permission.requiredPermission == Permissions.None) { return(true); } LinkedDiscordUser discordUser = (LinkedDiscordUser)user; if (channel == null) { return(false); } LinkedDiscordChannel discordChannel = (LinkedDiscordChannel)channel; return(discordUser.DiscordMember.PermissionsIn(discordChannel).HasFlag(permission.requiredPermission)); } return(false); }